node.js - koajs和react开发,koa如何把router的权利交给react?
PHPz
PHPz 2017-04-17 14:27:27
0
6
572

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

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

react 处理 '/user'

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

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

这种情况怎么办?

PHPz
PHPz

学习是最好的投资!

reply all(6)
大家讲道理

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

迷茫

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

伊谢尔伦

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

巴扎黑

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

左手右手慢动作

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

左手右手慢动作

KOA points to index.html when 404 occurs

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template