Node.js学习(15)----Express 3.x和Express 2.x区别
Node.js中Express框架变化还是挺大的。
学习一个还未成熟的技术还是有很大风险的,但也具有挑战。
Express 2.x过渡到Express 3.x
删除的:
1: res.render() "status" option (use node's res.statusCode= or res.status(code).render(...)) 2: res.render() "charset" option (use res.charset=) 3: res.local(name, value) (use res.locals.name = value or res.locals({ name: value }) instead) 4: app.dynamicHelpers() (use middleware + res.locals) 5: app.helpers() (use app.locals) 6: the concept of a "layout" (template engine specific now) 7: partial() (template engine specific) 8: res.partial() 9: "view options" setting, use app.locals 10: "hints" setting 11: req.isXMLHttpRequest (use req.xhr) 12: app.error() (use middleware with (err, req, res, next)) 13: req.flash() (just use sessions: req.session.messages = ['foo'] etc) 14: connect-flash can be used as middleware to provide req.flash()
变化的:
1: req.header(field[, defaultValue]) replaced by req.get(field) (remains for backwards compatibility) 2: res.header(field[, value]) replaced by res.set(field, value) / res.get(field) (remains for backwards compatibility) 3: res.send(body[, code]) is now res.send([code,] body) 4: res.redirect(url[, code]) is now res.redirect([code,] url) 5: res.json(obj[, code]) is now res.json([code,] obj) 6: renamed app.register() to app.engine() 7: template engine compliance from engine.compile(str, options) => Function to engine.__express(filename, options, callback) 8: express.createServer() is now simply express() (but remains for BC) 9: Use express.cookieParser('secret') instead of passing the secret to the express.session() middleware. The 'secret' option in the express.session() middleware has been removed.