Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

自己动手写todolist的后端 #52

Open
youngwind opened this issue Mar 29, 2016 · 1 comment
Open

自己动手写todolist的后端 #52

youngwind opened this issue Mar 29, 2016 · 1 comment
Labels

Comments

@youngwind
Copy link
Owner

趁着团队react培训的机会,我写了一个todolist的后端项目,以供前端接口调用。仓库地址:https://github.com/youngwind/todo-backend

开发一个基本的node后端所需要的基础知识在之前的博客中已经大部分提到了,大概整理罗列如下。

  1. nodejs异步编程的思想
  2. express框架的使用、路由、返回
  3. 中间件的编写(普通中间件与错误处理中间件)
  4. promise、ES6 generator、yield、co
  5. 操作数据库,mysql包,orm的使用(比如sequelize)
  6. redis内存存储

项目还有很多问题有待优化,比如:

  1. sequelize查询数据返回结果的解析还是不够优雅,而且没有捕获查询可能会发生的错误
  2. token是由时间戳+用户名构成,似乎不够规范
  3. 如何减少全局变量(想redis这样的真不知道怎么办)
  4. 数据表的定义并没有采用schema,而是直接采用sequelize,通用性不强
  5. ……

参考资料:

  1. http://www.expressjs.com.cn/
  2. http://sequelize.readthedocs.org/en/latest/docs/getting-started/

更新

跨域问题

自己在使用这个todo-backend的时候发现请求跨域了,一般解决方案如下

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();
});

本质上是一个中间件,应该有第三方成熟的工具来完成这事儿。我找到了cors

var cors = require('cors');
app.use(cors());

使用起来非常方便。

参考资料:http://www.tuicool.com/articles/vYBR3y

@youngwind youngwind changed the title 自己动手写todolist 自己动手写todolist的后端 Mar 29, 2016
@william-xue
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants