In the docs for express node.js module, the example code has app.use(....
. What is the use
function and where is it defined?`
The app object is instantiated on creation of the Express server. It has a middleware stack that can be customized in app.configure(). app.use() can be invoked there, and it will add onto your Express middleware stack.
E.g., 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. Each layer is essentially adding a function that specifically handles something to your flow through the middleware.
E.g. by adding bodyParser, you'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).
Hope that helps and happy node-ing!