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
|
6 Answers
Reset to default 21This 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
console.log(req.url)
to the middleware. I bet the browser is asking for favicon. – Yury Tarabanko Commented Nov 30, 2018 at 12:05