关于react项目与node后端共用80接口

来源:互联网 发布:网络用语蛤蟆啥意思 编辑:程序博客网 时间:2024/06/06 17:54

工作中因为某些原因需要后端api 必须使用80端口,或者为了美观..不使用nginx的情况下因为安全性,默认阻止跨域的话需要将前后端项目部署在同一server上。

react前端情况

使用browserHistory,使用webpack打包、redux、支持按需加载等特性。
规定除/api 路径外所有路径指向react

node后端情况

使用koa-router,若路径为/api 开头则调用后端api

实现方法

const router = require('koa-router')();const index = require('./routes/index');const api = require('./routes/api');// 优先级参照加载顺序router.use('/api', api.routes(), api.allowedMethods());router.use('*', index.routes(), index.allowedMethods());app.use(router.routes(), router.allowedMethods());

前后端完全分离,还是作为两个项目分开部署。例如:用webpack将生产环境前端代码生产到本项目/dist 路径下,后端koa 项目下只需要将./routes/index 所有路径无脑映射到前端项目/dist/index.html 就好了。
前后端项目还是分开部署,人员工作相互不影响,又可以共用80端。

0 0
原创粉丝点击