Node.js - require without assignment

Viewed 852

I wrote a small express application and put my routes in a different file, routes.js:

module.exports = function(server) { // Server is my express object
  server.get('/something', (req, res) => {
    // Stuff
  });

  // Some other routes
}

To use them in my main file, server.js, I require them like this:

require('./routes')(server);


This works fine, but I've never seen a require without an assignment like

const bla = require('some-module');.

Is the way I'm using require here even valid and / or a good practice?

2 Answers

Is the way I'm using require here even valid

Of course.

You're just calling the function immediately instead of storing it in a variable.

a good practice?

That's a matter of opinion.

The basic thing require does is that it executes code written in the module. At the end this executed code might or might not return something. In your case it doesn't matter what this code returns, rather what matters is that this code is executed at least once.

Yes its a good practice.

Related