2

I am trying to learn Express for NodeJS but I came across this:

I am trying to add 2 middlewares depeding on url, so on the /user to do something and on root to do something different. However the root middleware is always called even if i dont use next() and if i access the "/" url, the root middleware is called twice.

const express = require('express');

const app = express();


app.use('/user', (req, res, next) => {
    console.log('In user middleware ');
    res.send('<h1>Hello from User page</h1>');
});

app.use('/', (req, res, next) => {
    console.log('In slash middleware !');
    res.send('<h1>Hello from Express !</h1>');
});

app.disable('etag');
app.listen(3000);
Angelo
  • 195
  • 1
  • 15

4 Answers4

4

it should be get or post not use

-get or post are routes

-use is middleware function

check this

 const express = require('express');

  const app = express();


  app.get('/user', (req, res, next) => {
      console.log('In user middleware ');
      res.send('<h1>Hello from User page</h1>');
  });

  app.get('/', (req, res, next) => {
      console.log('In slash middleware !');
      res.send('<h1>Hello from Express !</h1>');
  });

  app.disable('etag');
  app.listen(3000);
Ahmed Ghoniem
  • 661
  • 1
  • 8
  • 15
  • I am following a video tutorial and somehow that code works for him but not for me? – Angelo Dec 16 '18 at 22:08
  • 1
    So the tutorial is not a good one, this answer is good and simple, if you want a better example just check my answer on another question https://stackoverflow.com/questions/53138548/how-to-app-use-all-routes-from-different-file/53141493#53141493 – Pedro Silva Dec 16 '18 at 22:20
1

From an issue at GitHub.com

https://github.com/expressjs/express/issues/3260

Hi @davidgatti my "root path middlware" I assume you are talking about nr_one. If so, yes, of course it is executed on every request; app.use is a prefix-match system. Every URL starts with /, so anything mounted at / will of course get executed :)

Will Ward
  • 1,886
  • 1
  • 10
  • 11
0

Okay, I can't confirm this but I suspect from the tutorial you are following you might be missing a line.

As you said, app.use is a middleware which will be added to all the route

So when you load say some url where you expect the middleware then it won't know about the request type (post, put, delete or get request).

Any alternate for this could be to try something like this

 app.use('/user', (req, res, next) => {
     if (req.method === 'GET') {
      console.log('In user middleware ');
      res.send('<h1>Hello from User page</h1>');
} 
  });

Again, Just check and compare his code thoroughly

Adding this link from Justin's answer for reference

Alwaysblue
  • 9,948
  • 38
  • 121
  • 210
-1

In order to avoid such a problem, you have to use the return keyword.

When you send a file to the server, use return.

Try the following code,

const express = require('express');

const app = express();


app.use('/user', (req, res, next) => {
    console.log('In user middleware ');
    return res.send('<h1>Hello from User page</h1>');
});

app.use('/', (req, res, next) => {
    console.log('In slash middleware !');
    return res.send('<h1>Hello from Express !</h1>');
});

app.disable('etag');
app.listen(3000);

At line 13 and 8, I used the return keyword.

So when you make http://localhost:3000/ request, you will receive

Hello from Express !

And whenever you make http://localhost:3000/user request, you will receive

Hello from User page

Shams Nahid
  • 6,239
  • 8
  • 28
  • 39