1

While creating server.js file we need to provide routes information as

var Login = require('./routes/login.js');
app.use('/login', Login);

Now, suppose the node project is a REST services project. so this type of entries could extend to lots of lines , meaning for every route we need to provide the above given 2 lines of code in server.js.
Similarly, if there is lots of 'require' statements which are needed to start the server and if there is some code related to middle ware , routing mechanism, analytics mechanism etc. etc.

How would it, then, be possible to keep the server.js file crisp(keeping it short and simple) and easy to understand?

How and what design pattern I should follow to make server.js beautiful?

  • 3
    You should search (both google and stackoverflow) for 'dynamic routing'. – Display name Oct 27 '16 at 8:36
  • Beauty is subjective. – Robert Harvey Oct 27 '16 at 15:27
  • This seems like an on-topic (if basic) question for SE. Why all the down votes? – GrandmasterB Oct 27 '16 at 15:40
  • @Orangesandlemons. Ok, i will search about it . but still I would like to discuss about this question as I am more interested in keeping the server js file as much modularized and as it can be . like keeping middle ware to one js , routing to another js , security token development to another js file and just referencing all of them to server js or connecting everything in app object kind of . I hope you understand my question . – Gaurav Shukla Oct 28 '16 at 8:48
  • @GauravShukla I don't know how proficient you are with node, but if you want to split up code you can (where it makes sense to do so) make your own files, require the relevant modules within them, and then use module.exports to require that in server.js. But first you need to make sure you have a clear idea of what the code is doing. – Display name Oct 28 '16 at 9:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.