Skip to content

Instantly share code, notes, and snippets.

@chenzx
Created October 22, 2014 10:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chenzx/4f32436270eca4398c92 to your computer and use it in GitHub Desktop.
Save chenzx/4f32436270eca4398c92 to your computer and use it in GitHub Desktop.
Node.js实战
Node.js实战
跳转至: 导航、 搜索
Node基础
DIRT
Browserling https://browserling.com
StackVM https://github.com/substack/stackvm
module.exports
p53 用闭包冻结变量的当前状态值:(function(c){...})(arg1);
串行/并行流程控制
p71 res.setHeader("Content-Length", Buffer.byteLength(body));
Stream.pipe()
p77 所有继承了EventEmitter的类(如stream)都可能发出error事件
querystring.parse()
用formidable处理文件上传:
var form = new formidable.IncomingForm();
form.on('file', function(name, file){ ... }); //其他事件:field, progress
https(略)
数据库:mysql / node-postgres
NoSQL:redis / MongoDB
提高性能:hiredis
Connect
p114 为什么中间件的顺序很重要:connect().use(logger).use(restrictFileAccess).... next()
挂载(集成了URL路由?)
可配置中间件*
router
p122 你可以利用闭包在外层函数中缓存正则表达式
rewrite
errHandler: function(err, req, res, next){ ... }
Connect自带的中间件:略
解析:cookieParser bodyParser limit query
logger favicon methodOverride vhost session
安全:basicAuth csrf errorHandler\
静态文件:static compress directory
Express
__dirname
res.render()
视图查找:index.ejs
p180 res.sendfile/download
bcrypt:genSalt()
特定路由的中间件校验
app.post('/post', requireEntryTitle, requireLengthAbove(4), entries.submit);
分页
API设计:app.get('/', page(Entry.count, 5), entries.list);
--> '/:page?'
REST
User.prototype.toJSON = ...
启用内容协商
错误处理
测试Node:内置assert nodeunit Mocha(可检测全局变量泄露?) Vows should.js
验收测试:Tobi(模拟浏览器,htmlparser) Soda(远程控制,Selenium RC)
Web模板(EJS)
如果不想转义模板参数:var template = '<%- msg %>';(默认的<%=会转义以防止XSS)
过滤器:<%=: title | truncate:20 %>
Mustache与Hogan(略)
{{ }}的风格来自django?
Jade(这个倒是有点意思,类似于yaml)
部署
Socket.IO
修改CSS触发浏览器重新加载
OS交互
child_process
exec():缓冲命令结果
spawn():繁衍带有流接口的命令
利用fork()分散负载
生态系统
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment