0

I observed a wierd behavior on my NodeJS server running on my localhost (my macbook). Initially I have my wifi on (Internet is ON), then I send a request to my server GET /jobHistory -> All the request to static files are logged (using morgan JS), and I can see it takes about 300-400ms for every static files.

I turn OFF the wifi (Internet is OFF), I have to restart my NodeJS server (otherwise it will be idle - not sure why). I send another GET /jobHistory -> Now all the request to static files only takes about 5ms-10ms per file, much faster than what I observed.

Screenshot

If you have any clue on this, please enlighten me, or tell me how could I investigate further. This seems to be some important point about NodeJS server that I missed...

HenryNguyen
  • 1,153
  • 1
  • 10
  • 8

1 Answers1

0

I figured out what was the problem:

My middleware to serve static files were BELOW the middleware of cookies, favicon, cookiesparser, logger, session.

// Route to public static files (js, css)
app.use(express.static(path.join(__dirname, '../public')));

so express was taking time to run some middleware functions (possibly query DB/cache for session information) before serving me the file.

The fix was simply move my static file middleware UP, before other middleware. The result is amazing: each static file is served within 5-7ms.

HenryNguyen
  • 1,153
  • 1
  • 10
  • 8