DEV Community

Discussion on: Always separate app and server files !

Collapse
 
sonimonish00 profile image
Monish Soni

I had the same doubt earlier, what i think is that it is better to put Mongodb atlas code in server.js bcz in real world scenario, http server needs to connect to db first and then start which would be more convenient if u put it in server.js rather than app.js. Also put MongoDB Connection as separate module/file.
Following is my folder structure :

Backend

  • App (Follows MVC)
    • index.js (Contains express app code & calls to MVC part)
    • Controller
    • Middlewares
    • Models
    • Routes
  • Database (MongoDB Atlas/mongoose Connection)
    • db.config.js
  • Env (Environment variable prod/dev - For mongodb url, etc.)
    • .env
  • server.js (Main entry point - code related to http server, mongodb conn etc. here)
Collapse
 
florianwaltherprivate profile image
Florian Walther

Thank you, that makes sense! Especially the point about starting the server after the connection was successful.