what is app.use in NodeJS/Express

In the docs for the NodeJS express module, the example code has app.use(...).

What is the use function and where is it defined?

share|improve this question
 
2  
possible duplicate: stackoverflow.com/questions/7337572/… – Alexander Bird Jul 4 '12 at 2:30

2 Answers

up vote130 down voteaccepted

The app object is instantiated on creation of the Express server. It has a middleware stack that can be customized in app.configure()(this is now deprecated in version 4.x).

To setup your middleware, you can invoke app.use(<specific_middleware_layer_here>) for every middleware layer that you want to add (it can be generic to all paths, or triggered only on specific path(s) your server handles), and it will add onto your Express middleware stack. Middleware layers can be added one by one in multiple invocations of use, or even all at once in series with one invocation. See use documentation for more details.

To give an example for conceptual understanding of Express Middleware, here is what my app middleware stack (app.stack) looks like when logging my app object to the console as JSON:

stack: 
   [ { route: '', handle: [Function] },
     { route: '', handle: [Function: static] },
     { route: '', handle: [Function: bodyParser] },
     { route: '', handle: [Function: cookieParser] },
     { route: '', handle: [Function: session] },
     { route: '', handle: [Function: methodOverride] },
     { route: '', handle: [Function] },
     { route: '', handle: [Function] } ]

As you might be able to deduce, I called app.use(express.bodyParser())app.use(express.cookieParser()), etc, which added these express middleware 'layers' to the middleware stack. Notice that the routes are blank, meaning that when I added those middleware layers I specified that they be triggered on any route. If I added a custom middleware layer that only triggered on the path /user/:id that would be reflected as a string in the route field of that middleware layer object in the stack printout above.

Each layer is essentially adding a function that specifically handles something to your flow through the middleware.

E.g. by adding bodyParseryou're ensuring your server handles incoming requests through the express middleware. So, now parsing the body of incoming requests is part of the procedure that your middleware takes when handling incoming requests -- all because you called app.use(bodyParser).

share|improve this answer
 
39  
thank you. would it be so difficult for the express folks to explain this in their API docs? – ericsoco Jun 25 '13 at 21:52
3  
So you're saying that when a request is received the data is passed through those parsers before hitting the actual service. So for example: Valid Request->Authentication->ProcessesRequest->ServResponse USE would control those steps in a specific order and not execute them parallel? – CyberMen Oct 10 '13 at 19:10
2  
When is the function that is sent to app.use called? After creating the express server or for every request? – Timo Huovinen Nov 21 '13 at 18:25
1  
You should update this answer because apparently app.configure was removed from express in the last release. – nbro Oct 21 '15 at 11:35
1  
Projects generated by express use app.use to load routes. I don't consider my routes middleware, so is this another acceptable use case? – Jon Dec 9 '15 at 9:08

use is a method to configure the middleware used by the routes of the Express HTTP server object. The method is defined as part of Connect that Express is based upon.

share|improve this answer
 
2  
3  
And the online docs are here: senchalabs.org/connect/proto.html#app.use – Alexander Bird Jul 4 '12 at 2:47
 
Would it be efficient if a Node middleware instantiates objects? Would this mean that on every request, that middleware instantiates new objects? Do the old objects get discarded? For example app.use(function(){ var object = new SomeConstructor; next(); }) – CMCDragonkai Mar 6 '14 at 14:01
1  
Short and sweet. – mishap Dec 13 '15 at 1:41
1  
@CMCDragonkai It's fine to instantiate objects on every request. They'll get garbage collected so long as you're not storing references to the objects outside the scope of your request handler. – jeff_mcmahan Dec 16 '15 at 2:45
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章