Home > Software design >  multiple server calls when creating a simple node server
multiple server calls when creating a simple node server

Time:12-02

issue: I am seeing 3 sets of console logs when I visit http://localhost:3000/

expected output: I should only be seeing two console.logs:: First middleware Second middleware

I have the following two files:

server.js ::

const http = require('http');
const app = require('./backend/app');

const port = process.env.PORT || 3000;

app.set('port',port);
const server = http.createServer(app);

server.listen(port);

and app.js::

const express = require('express')

// big chain of middle ware
const app = express();

app.use( (req,res,next)=>{
    console.log('First middleware');
    next();
});

app.use( (req,res,next)=>{
    console.log('Second middleware');
    res.send('Hello from express');
});

// register what you want to export
module.exports = app;

for some mysterious reason, I when I visit http://localhost:3000/ I see three sets of console logs in my sever console::

First middleware
Second middleware
First middleware
Second middleware
First middleware
Second middleware

I page is not reloading 3 times lol

I can see the output 'Hello from express' when I visit local host3000. but I am not sure why this code is running three times

I tried to look for other similar questions like this but I did not see anything similar

CodePudding user response:

This could potentially be due to loading other files on the server, such as stylesheets or javascript files. Remember that all requests no matter file type or path will be routed through your middleware. You can also try logging the request path with <Request>.originalUrl string property.

CodePudding user response:

I think it has something to do with the google chrome extensions. When I open the browser on private. it works perfect.

  • Related