6

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) {
alditis
  • 4,633
  • 3
  • 49
  • 76
The Learner
  • 3,867
  • 14
  • 40
  • 50

1 Answers1

8

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.

Brad
  • 159,648
  • 54
  • 349
  • 530
  • 1
    Thank you for the explanation. so "app.use(), you pass in a function to handle requests" so in my examples I have no request only the function to be called . so it will take it default "GET"??? ..yes count is a userdefined function as function count(req, res). can you please explain when app.use(express.cookieSession()); is called? my question here is there is no req. so on what basies that will be called – The Learner Dec 23 '12 at 07:46
  • What do you mean? You're not using Express to handle HTTP requests? Middleware loaded up with `app.use` will be called no matter what the request is. That doesn't mean it will always act on the request... it's up to the middleware to decide whether to handle the request or let the next function handle it. – Brad Dec 23 '12 at 07:48
  • I have updated the question with a sample program. my question is when a Request comes which "app.use" should be called. I see that in my case it always calls app.use(count) . I am asking why not : app.use(express.cookieSession()); – The Learner Dec 23 '12 at 07:54
  • 1
    They are all called, in order that they were set up with `app.use`, until one piece of middleware decides to handle the request. Your `express.cookieSession` is being called if you are reaching `count`. See also: http://stackoverflow.com/a/8711139/362536 – Brad Dec 23 '12 at 07:58
  • ok now I am clear ..my last question : in "app.use(express.cookieParser('manny is cool'));", I do not have next. I saw the defination in the path you provided : module.exports = function cookieParser(secret){ req is what the message is received. if it have cookie as per defination it will next. where is the next value? where does it go ? infact I see that app.use(count); is called in my example which is after the cookie – The Learner Dec 23 '12 at 08:09
  • I was looking at ur previous reply again . so app.use(express.cookieParser is called and this will be having next that is not define and it calls the next available app.use right? – The Learner Dec 23 '12 at 08:15
  • That is correct. When you call `next`, it calls the next function. – Brad Dec 23 '12 at 08:35
  • Thank you again. I am clear and last question before I close this. I read in the link you provided next will call the one in Express-stack.so, in the code if I have :app.use(express.cookieParser('manny is cool')); app.use(express.cookieSession()); app.use(count); means in the stack when it Reads each one is kept in the stack and once it does not find the action it should do in the fisrt function will call the second one in this case : express.cookieSession() right? – The Learner Dec 24 '12 at 03:39
  • 1
    Yes, that's correct. It is up to the middleware to decide whether or not to call the next function. Middleware, such as the cookie handlers you have, will always call the next function. – Brad Dec 24 '12 at 04:56
  • Brad, the question is : middle will call next in the express stack. is the express stack updated with the way the code is written. say , we have app.use(express.favicon()); app.use(express.cookieParser('manny is cool')); app.use(express.cookieSession()). so the stack in express which middleware looks is : first it will see express.favicon(), than express.cookieParser than express.cookieSession() because they are written in the code seqently and read happens and it is stack is updated accordinly? – The Learner Dec 24 '12 at 05:08
  • What do you mean by "updated"? The stack never changes. It is always in the same order it was set up in. There is nothing to update or change about the stack. It's just a list of callback functions that are always called in the specific order they were installed with `app.use`. – Brad Dec 24 '12 at 05:15
  • In the example I gave in question I see that when a new req comes it is going to "app.use(count);" where count is a user defined function.I feel that I have app.use(express.favicon()); first as my request is not favicon this is ignored. than the next one is : express.cookieSession(). this also does not handle the requests its just creating/initiating a cookiesession, than it is going to count. I was thinking when a request comes as these are all independent app.use. the atck will go over each one by one and when the request is not meet , it will call next that is how it is pushing – The Learner Dec 24 '12 at 05:24
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/21622/discussion-between-brad-and-the-learner) – Brad Dec 24 '12 at 05:28