Here are three pieces of terminology used in documentation relating to ConnectJS for NodeJS that keeps getting used, but that I don’t completely undertand:
1) views and controllers
2) partials and collections
3) Middleware
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Let’s start from the bottom up.
Level 0: built-in http module
In the beginning, there is node.js’s built-in http.Server written by Ryan Dahl. You write a
function(req, res), and Node will call your function each time a new connection is accepted:Level 1: Connect
Connect, written by Tim Caswell, is simply a subclass of http.Server that makes it easier to organize your code. Instead of writing a single callback that handles every request, you chain together some middleware. Each middleware is a
function(req, res, next)that handles the request if possible, or callsnext(error)if it did not finish handling the user’s request. The middleware handlers are called in the order of theiruse; you should call the catch-allapp.use(connect.errorHandler())at the end.One important middleware is the router, which allows you to filter some middleware based on a pattern of the URL path. The syntax for the route patterns is based on ruby’s Sinatra routes. When I use the filter
/hello/:name,req.params.namewill be set to the matching part of the URL.In Connect, every handler is middleware! You use whichever functionality you need like bodyParser or cookieParser, and your own business logic is also a middleware function with the same signature
function(req, res, next). The connect homepage gives a list of the built-in middleware.Level 2: Express.js
Express’s http server, written by TJ Holowaychuk, is in turn a subclass of Connect that forces the Sinatra style more. In Connect, there was no magic you didn’t ask for, but in Express, the router and qs parser (which sets
req.query) are automaticallyused. The router syntax is cleaned up; you callapp.get,app.post, etc. directly (and the router is positioned at the first call) rather than putting them inside a function.Express also contains many other well-documented features and helper functions to extend app, req, and res.
One feature of Express is
res.render, which renders the given template file (relative toapp.set('views')or $PWD/views) using the template engine implied by the extension, andres.partial, which calls render on each element of a collection (which is just any arraylike object). But I haven’t used this optional feature; if you don’t care for express’s templates you can justres.senddata yourself.