Willem van der Veen
Willem van der Veen

Reputation: 36650

Express performance requiring modules

Having a question regarding the performance of a NodeJS application. When I have the following express app in NodeJS:

const app = require('express')();
const about = require('./about');

app.use('/about', about);

app.listen(3000, () => console.log('Example app listening on port 3000!'));

My current understanding is that only when the server is started up it does need to require() these files using commonJS modules.

Question:

Does the express application has to execute the require() statements with every request to the server or is this only necessary when starting the server?

Any extra information about how express works under the hood would be nice.

Upvotes: 0

Views: 43

Answers (1)

yBrodsky
yBrodsky

Reputation: 5041

No, those require are only run once when you start the app. It would be different if you include those in the router functions.

app.use('/about', (req, res) => {
  const some = require('some');
});

Still in this scenario, modules require are cached so it's not such a big deal.

Upvotes: 3

Related Questions