关于express的视图助手和路由分配
发布于 12 年前 作者 carlos121493 7437 次预览 最后一次回复是 12 年前 来自
我是一个刚开始学习nodejs的新手, 在读完nodejs开发指南的时候感觉nodejs很强大 不过,当真正开始做的时候发现书中的express里面 关于视图助手部分和将路由分出来的功能已经不能用 而且在链接mongodb的时候也是不行.
看了官方文档将里面的内容看了一遍也没发现什么视图助手的部分 搁浅了一阵子,想请教一下用express框架视图助手和路由分出的功能应该怎么做才是?
3 回复
我也在看这本书,这本书写作是在2012年左右,在这几年里express 有了很大的变化,比如 layout 和 partical 都在最新的3.X版里面去掉了。你可以联系这本书的作者 BYVoid 来解答一下。他的博客联系方式是:http://www.byvoid.com/contact
技术永远都是快速发展的
https://github.com/visionmedia/express/wiki/Migrating-from-2.x-to-3.x
Migrating from 2.x to 3.x
Removed
res.render()“status” option (use node’sres.statusCode=orres.status(code).render(...))res.render()“charset” option (useres.charset=)res.local(foo, bar)(useres.locals.foo = barorres.locals({ foo: bar })instead)app.dynamicHelpers()(use middleware +res.locals)app.helpers()(useapp.locals)partial()(template engine specific)res.partial()app.localsreq.isXMLHttpRequest(usereq.xhr)app.error()(use middleware with (err, req, res, next))req.flash()(just use sessions:req.session.messages = ['foo']or similar)jsonp callbacksetting was removed (useres.jsonp())Changed
req.header(field[, defaultValue])replaced byreq.get(field)(remains for backwards compatibility)res.header(field[, value])replaced byres.set(field, value)/res.get(field)(remains for backwards compatibility)app.register()toapp.engine()engine.compile(str, options) => Functiontoengine.__express(filename, options, callback)express.createServer()is now simplyexpress()(but remains for BC).express()is no longer anhttp.Serverinstance. (See the Application function section below for more details)View options
The “view options” setting is no longer necessary,
app.localsare the local variables merged withres.render()'s, soapp.locals.pretty = trueis the same as passingres.render(view, { pretty: true }).Application function
The return value of
express()is a JavaScriptFunction, encapsulating everything that makes an Express app tick. This means you can easily setup HTTP and HTTPS versions of your application by passing it to node’shttp.createServer()andhttps.createServer():For convenience, and smaller applications the
app.listen()method takes the same arguments, wrapping in an HTTP server. The following are equivalent:and
This however means that methods that are on node’s
http.Server.prototypeare no longer present onapp, for exampleapp.address()must now be called on the server returned byapp.listen()or the one you have wrapped withhttp.createServer(app).Socket.IO compatibility
Socket.IO’s
.listen()method takes anhttp.Serverinstance as an argument. As of 3.x, the return value ofexpress()is not anhttp.Serverinstance. (See the Application function section above.) To get Socket.IO working with Express 3.x, make sure you manually create and pass yourhttp.Serverinstance to Socket.IO’s.listen()method.Template engine integration
Express 2x template engine compatibility required the following module export:
Express 3x template engines should export the following:
If a template engine does not expose this method, you’re not out of luck, the
app.engine()method allows you to map any function to an extension. Suppose you had a markdown library and wanted to render.mdfiles, but this library did not support Express, yourapp.engine()call may look something like this:View system changes
By removing the concept of a “layout” & partials in Express 3.x template engines will have greater control over file I/O. This means integration with template engines much easier, and greatly simplify the view system’s internals.
This also enables template engines to supply their own means of inheritance, for example later releases of Jade provide Django-inspired template inheritance, where the view being rendered specifies the layout it wants to extend. For an example of this using the Jade engine visit http://www.devthought.com/code/use-jade-blocks-not-layouts/
Post-release we may end up building an Express extension to support the old
partial()concept.To get back layout functionality with EJS you can use express-partials or ejs-locals.
Error handling middleware
The
app.error(callback)method in 2.x was effectively the same as the following:The reason for this is that Connect differentiates between “regular” middleware, and “error-handling” middleware via the
fn.length. A regular middleware has afn.lengthof<= 3, aka(req, res, next), whereas error-handling middleware must have exactly4(err, req, res, next). So the reason 2.x wrapped this functionality was to simply provide a bit of sugar on-top of this API making the parameters optional.In short all you need to do to “catch” these errors that are passed along is to define another middleware, but with
4arguments. Note that this middleware should be defined below all the others, so that they may invokenext(err)in order to pass an error to it like so:App- & Request-level local variables
Do not use
nameas a key in app.locals or res.locals because those objects are Function object instances. Any attempt to set them will be silently ignored. Other unstable or unusable top level keys are listed here: Function Instance Properties