最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Nodejs with express : Why my middleware is executing twice Here is the code - Stack Overflow

programmeradmin2浏览0评论

This is my code here..!

const express = require('express');
const app = express();

let myFunc = function (req, res, next) {
    console.log('This is middleware');
    next();
}

app.use(myFunc);

app.get('/', (req, res) => {
    console.log('This is get /');
    res.send('Hello World!');
});

app.listen(3000, () => {
    console.log('Server is running at port 3000....');
});

In this I have created a middleware called myFunc but the output is not as I thought it would be

Server is running at port 3000....
This is middleware
This is get /
This is middleware

This is my code here..!

const express = require('express');
const app = express();

let myFunc = function (req, res, next) {
    console.log('This is middleware');
    next();
}

app.use(myFunc);

app.get('/', (req, res) => {
    console.log('This is get /');
    res.send('Hello World!');
});

app.listen(3000, () => {
    console.log('Server is running at port 3000....');
});

In this I have created a middleware called myFunc but the output is not as I thought it would be

Server is running at port 3000....
This is middleware
This is get /
This is middleware
Share Improve this question asked Nov 30, 2018 at 12:03 Aditya PradhanAditya Pradhan 1031 silver badge5 bronze badges 3
  • 3 Add console.log(req.url) to the middleware. I bet the browser is asking for favicon. – Yury Tarabanko Commented Nov 30, 2018 at 12:05
  • 3 I think the other request is for favicon – Prakash Sharma Commented Nov 30, 2018 at 12:05
  • the other one is for favicon! – Amir Hossein Commented Mar 5, 2021 at 20:45
Add a comment  | 

6 Answers 6

Reset to default 21

This happens because the browser is requesting two files. The first will be for / and the second is likely for favicon.ico. If you want to see why this is being called change your one function to look like this:

let myFunc = function (req, res, next) {
  console.log('This is middleware', req.originalUrl);
  next();
}

Then it will output the URL that was requested for each time the browser hits the server.

app.use is running every time you trigger app. in this case you trigger twice. app.get and app.listen

This happens because your browser automatically sends a request to /favicon.ico URL. console.log(req.url) will show you what's happening.

let myFunc = function (req, res, next) {
    console.log('This middleware was triggered for the URL:' + req.url);
    next();
}

Try sending requests to the server using postman as it does not send anything automatically.

Your static files also share the same url as the one u are trying to use. So most likely you will have something like this: localhost:3000/:variable

Here this url will match a lot of stuff

As @DavidWhite said app.use is running every time you trigger app. in this case you trigger twice. app.get and app.listen You should use the middleware this way

const express = require('express');
const app = express();

let myFunc = function (req, res, next) {
    console.log('This is middleware');
    next();
}

app.get('/', myFunc,(req, res) => {
    console.log('This is get /');
    res.send('Hello World!');
});

app.listen(3000, () => {
    console.log('Server is running at port 3000....');
});

When you use app.use(myFunc); you apply middleware to all app. And you are using app.get and app.listen, that's why the middleware triggered twice. Try to use middlewere in get query

发布评论

评论列表(0)

  1. 暂无评论