Anders Östman
Anders Östman

Reputation: 3832

What is ending a route

Supose i have this route code:

app.get('/', function(req, res) {
    res.send('Hello world!');
});

app.get('/something', function(req, res) {
   // Do something
});

If i visit the / route in my browser, the "Hello world" message will show, and my response will have ended.

Question: Is it the absence of next() in this router, or the res.send() that ENDS the http request?

Upvotes: 0

Views: 45

Answers (2)

cgwic
cgwic

Reputation: 52

Sorry, but if you want to log every request including '/' then you have to reorder using:

app.use(function(req, res, next) {

    // log each request to the console
    console.log(req.method, req.url);

    // continue doing what we were doing and go to the route
    next();
});

app.get('/', function(req, res) {
    res.send('Hello world!');
});

Upvotes: 0

Ben Fortune
Ben Fortune

Reputation: 32117

It's res.end() that actually ends the response, but res.send() calls this.

You can see this in the source here.

Upvotes: 2

Related Questions