0
const express = require('express');

const app = express();

app.use('/', (req, res, next) => {
    console.log('This always runs!');
    next();
});

app.use('/add-product', (req, res, next) => {
  console.log('In add product middleware!');
  res.send('<h1>The "Add Product" Page</h1>');
});

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

app.listen(3000);

NodeJS / Express: what is "app.use"? I read from this post and still confused about how the flow-of-control goes in this program.How come if I visit "localhost:3000/add-product" the result logged is "This always runs!In add product middleware!This always runs!In another middleware!"(I omitted the changeline) Does this mean after it goes into the second app.use,and as I've learnt,each app.use(middleware) is called every time a request is sent to the server.So this process restarts,but why this time next() would result in the third app.use being called?I thought next would go into the next matching path..

  • It really depends on how specific the route you're calling. Try calling it with route `/add-product` and the call will go to 1st and 2nd `app.use` route Edit: The route should be arrange from most specific to least specific as well. since that is the order of matching. – PKiong Jul 25 '21 at 02:30
  • I tried calling it with route /add-product and the result logged to console is as I mentioned,which means it goes into the 1st and 2nd and then 1st and then 3rd,which is very confusing for me. – ansley77777723 Jul 25 '21 at 02:34
  • @ansley77777723 when you call for /add.... 1st Middleware runs and calls next for second. As you can see, second middleware is ending the req res cycle. We got the response. Again first middleware starts but this time it will go to the third one. because we have already got the response from second. You can read more about sub stack middlewares. – Ranveer Sequeira Jul 25 '21 at 02:52

1 Answers1

0

The order of route is important in express

Express match route based on first come first serve basis, just like a queue.

If a route matches, then whatever function you pass as callback will get called.

In your case: Route 1: Match every route Route 2: Match /add-product Route 3: Match every route

So the order of checking would be 1 -> 2 -> 3

So if I make GET CALL TO /add-product (1) and (2) will be called and the following log

This always runs!
In add product middleware!

While call to / will result in (1) and (3) being called.

This always runs!
In another middleware!

Next() is just passing the control to the next middleware

PKiong
  • 521
  • 4
  • 13
  • "This always runs! In another middleware! This always runs! In another middleware!"This is the result I get when I visit route/.Could you please explain why it would repeat twice?As I've read from the documentation I thought your logic is right,but it doesn't match with what I'm seeing,hence the confusion. – ansley77777723 Jul 25 '21 at 02:44
  • Maybe you want to push ur code to a repo so we can take a look at it? – PKiong Jul 25 '21 at 02:45
  • The only code I used is as above(in a single app.js file),I didn't use any other files.Could you please try it on your computer? – ansley77777723 Jul 25 '21 at 02:48
  • I've just tried the above code, it doesn't return twice. What do you use to run the server? `node` ? `nodemon` ? What do you use to make the `request`? postman? – PKiong Jul 25 '21 at 02:49
  • yes I use node,but I don't know what I use to make the request..I'm new to this and I'm following a tutorial about this and the teacher also get my confusing output but he seems happy about it,so.. – ansley77777723 Jul 25 '21 at 03:01
  • so you are calling ur backend using a browser? – PKiong Jul 25 '21 at 03:07