0

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.

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.