The Learner
The Learner

Reputation: 3927

Express app.use

I have been reading documents/urls and really not understand about app.use and its usage. I understand that it is part of connect but I am really not getting that.

Example:

// ignore GET /favicon.ico
app.use(express.favicon());
// add req.session cookie support
app.use(express.cookieSession());
// do something with the session
app.use(count);

can you please explain me all these 3 . what they mean? does this mean based on (1) that app.use is noting but => app.get? app.use(count) what and when is this count be executed (or) called/

Looks Basic but did not get the answers

// ignore GET /favicon.ico
app.use(express.favicon());

// pass a secret to cookieParser() for signed cookies 
app.use(express.cookieParser('manny is cool'));

// add req.session cookie support
app.use(express.cookieSession());

// do something with the session
app.use(count);

// custom middleware
function count(req, res) {

Upvotes: 6

Views: 6102

Answers (1)

Brad
Brad

Reputation: 163548

When you call app.use(), you pass in a function to handle requests. As requests come in, Express goes through all of the functions in order until the request is handled.

express.favicon is a simple function that returns favicon.ico when it is requested. It's actually a great example for how to get started with this pattern. You can view the source code by looking at its source: node_modules/express/node_modules/connect/lib/middleware/favicon.js

express.cookieSession is some more middleware for supporting session data, keyed from the client by a cookie.

I don't know what count does... is that your own code? In any case, let me know if this is not clear.

Upvotes: 8

Related Questions