koajs和react开发,如何控制路由?
因为koa和react都会处理路由,但是koa的权利更大,比如
react 处理 '/user'
而 koa 对'/user'没有特殊处理,也就是404
这时,直接访问'/user'就会得到 404,而得不到react的处理结果
这种情况怎么办?
大家讲道理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
伊谢尔伦2017-04-17 14:29:27
react-router is a front-end routing, it is different from koa's routing
巴扎黑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
ringa_lee2017-04-17 14:29:27
It shouldn’t matter, the front-end and back-end routing are not mentioned to be unified
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');
});