search

Home  >  Q&A  >  body text

node.js - koajs和react开发,koa如何把router的权利交给react?

koajs和react开发,如何控制路由?

因为koa和react都会处理路由,但是koa的权利更大,比如

react 处理 '/user'

而 koa 对'/user'没有特殊处理,也就是404

这时,直接访问'/user'就会得到 404,而得不到react的处理结果

这种情况怎么办?

PHPzPHPz2781 days ago573

reply all(6)I'll reply

  • 大家讲道理

    大家讲道理2017-04-17 14:29:27

    You can use route matching in react server rendering. There is a match method in react-router
    You can look at the code commented in https://github.com/zhanfang/koa-react-weixin. I think This is not very necessary. Another way is what I am doing now. You can refer to the code in the link

    reply
    0
  • 迷茫

    迷茫2017-04-17 14:29:27

    I found a way, it seems that koa does not handle 404, it feels good

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-17 14:29:27

    react-router is a front-end routing, it is different from koa's routing

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-17 14:29:27

    React Router official documentation mentions that if the front-end is routed, the back-end only needs to return the HTML file to the corresponding path. You can try

    reply
    0
  • ringa_lee

    ringa_lee2017-04-17 14:29:27

    It shouldn’t matter, the front-end and back-end routing are not mentioned to be unified

    reply
    0
  • ringa_lee

    ringa_lee2017-04-17 14:29:27

    KOA points to index.html when 404 occurs

    Reference
    router.get('*',(ctx, next) => {
    ctx.type = 'html';
    ctx.body = fs.createReadStream('./index .html');
    });

    reply
    0
  • Cancelreply