- Use
touch .env
to make .env file in root directory. - Run
npm install
. - Use
npm i nodemon -g
to install nodemon globally. - Configure your mongodb and start it.
- Use command
npm start
to start server in development mode.
-
The .env file is very important.
-
Edit its fields with the ones you use.
-
Example -
DB_URL='mongodb://localhost/dbname' HOST_URL='backend_url'
-
HOST_URL is useful when referencing site url in mails or anywhere in backend
- In the root directory there is app.js, config.js.
- app.js is for starting the server.
- config.js is for configuration of mongodb according to node env(dev or prod).
- The /app folder contains /controllers, /models, /routes, /utils.
- route files will map Api routes to controllers and validate recieved data.
- controller files have the main app logic.
- utility files will have simple functions which can be used in other places to avoid repetition.
- All the Api's are prefixed with /api/v1.
- Routes which have /:id in the end are used for manipulation of document with that mongo id.
- All models must be named like {resource}.js (Eg:- user.js)
- All controllers must be named like {resource}Ctrl.js (Eg:- userCtrl.js)
- All routes must be named like {resource}Routes.js (Eg:- userRoutes.js)
- Api patterns -
-
There can be 5 routes for each resource -
- GET /{resource} (Eg:- GET /posts) - GET /{resource}/:id (Eg:- GET /posts/:id) - POST /{resource} (Eg:- POST /posts) - PUT /{resource}/:id (Eg:- PUT /posts/:id) - DELETE /{resource}/:id (Eg:- DELETE /posts/:id)
Note that here we have to use the plural.
-
- Users
- Posts
- Comments
- Tags
- Courses