express icon indicating copy to clipboard operation
express copied to clipboard

The ".use" router after ".get" or ".put" doesn't work

Open StevenRoan opened this issue 11 years ago • 3 comments

In my understands, all the routes are applied based on the declared order, but I just found out ".use" route doesn't follow this concept. Example

var express = require('express');
var app = express();
app.listen(1339);
app.use(function (req, res, next){
    console.log('use 1');
    return next();
})
app.get('/get1', function (req, res, next){
    console.log('get 1');
    return res.json("hi1");
});

app.use(function (req, res, next){
    console.log('use2');
    return next();
})
app.get('/get2', function (req, res, next){
    console.log('get 2');
    return res.json('hi2')
});

If I use command curl -X GET http://127.0.0.1:1339/get2, the console.log will be:

use 1
get 2

and the use 2 is not printed, which declared before get2.

I am not sure if I misuse the api or I misunderstand the concept of routing in Express. If this is on purpose, can anyone share the concept behind this? Thanks a lot.

StevenRoan avatar Aug 28 '15 13:08 StevenRoan

Hi! What version of Express are you using?

Your results indicate you are using 3.x, which groups use at the top. It was not until 4.x that use also started running in declared order.

dougwilson avatar Aug 28 '15 13:08 dougwilson

Running your exact example above on Express 4.13.3 (the current version), seems to give the output you were expecting for me:

use 1
use2
get 2

Here is the full output from my session to reproduce your issue:

$ node -v
v0.10.33

$ npm i express
[email protected] node_modules\express
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected] ([email protected])
├── [email protected] ([email protected])
├── [email protected] ([email protected])
├── [email protected] ([email protected], [email protected])
├── [email protected] ([email protected], [email protected])
├── [email protected] ([email protected], [email protected], [email protected], [email protected], [email protected])
└── [email protected] ([email protected], [email protected])

$ cat app.js
var express = require('express');
var app = express();
app.listen(1339);
app.use(function (req, res, next){
    console.log('use 1');
    return next();
})
app.get('/get1', function (req, res, next){
    console.log('get 1');
    return res.json("hi1");
});

app.use(function (req, res, next){
    console.log('use2');
    return next();
})
app.get('/get2', function (req, res, next){
    console.log('get 2');
    return res.json('hi2')
});

$ node app.js &
[1] 7988

$ curl -s http://127.0.0.1:1339/get2
use 1
use2
get 2
"hi2"

dougwilson avatar Aug 28 '15 13:08 dougwilson

@dougwilson Thanks for pointing out the problem!

StevenRoan avatar Aug 29 '15 03:08 StevenRoan