app.all('*', function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
res.header("X-Powered-By",' 3.2.1')
res.header("Content-Type", "application/json;charset=utf-8");
next();
});
var cors = require('cors');
app.use(cors());
趁着团队react培训的机会,我写了一个todolist的后端项目,以供前端接口调用。仓库地址:https://github.com/youngwind/todo-backend
开发一个基本的node后端所需要的基础知识在之前的博客中已经大部分提到了,大概整理罗列如下。
项目还有很多问题有待优化,比如:
参考资料:
更新
跨域问题
自己在使用这个todo-backend的时候发现请求跨域了,一般解决方案如下
本质上是一个中间件,应该有第三方成熟的工具来完成这事儿。我找到了cors
使用起来非常方便。
参考资料:http://www.tuicool.com/articles/vYBR3y