My preferred Node.js backend project setup. Codebase is organized keeping in mind both development and production environments.
Basic familarity with Git, Node.js and npm.
Basic familarity with server-side application development using Node.js and Express.js.
Basic familarity with development and production environments for Node.js backend applications.
Server: Node.js & xpress.js
Database: MongoDB
Clone the project
git clone https://github.com/jaimin1618/nodejs-backend.git
Go to the project directory
cd nodejs-backend
Install dependencies
npm install
Setup environment variables & fill environment variables in .env file.
cp .env.example .env
Start the server
npm run server
-
Keep the server.js file as it is during development stage.
-
Configure environment variables .env and constants file config/constants.js
-
Organize endpoints/routes in multiple files & wrap the controller with AsyncHandler, it will deal with try {} catch(e) {} wrapping of async program errors.
router.use( "/", AsyncHandler(async (req, res) => { res.json( ApiResponse("Api Running Successfully.", null, StatusCodes.CREATED) ); }) );
-
Use common api response structure for all endpoints - as shown in below example.
res.json( ApiResponse("message", data, StatusCode) );
-
Use common api error response structure to throw errors - as shown in below example.
if (!authenticate) { throw new ApiError("Invalid credentials!", StatusCodes.UNAUTHORIZED, { credentials: { email, password }, }); }
-
Let the middleware handle BAD_REQUEST errors.
router.post( "/login", AuthRoutesValidations.loginValidation, // request validators HandleBadRequest, // BAD REQUEST - 400 middleware handler AuthController.login );
- Get the SSL certificate files chain.pem, privkey.pem and cert.pem and put them inside config/ folder. You can use Certbot CLI for getting SSL certificate from letsencrypt.
- Uncomment the lines with comment "for production use" inside server.js file.
- Configure environment variables inside .env file.
- Start the server with following commands.
npm run server
or using PM2 - Node.js process manager
sudo pm2 start server.js --name="server"
Contributions are always welcome! If you'd like to contribute to this project, please follow these guidelines to ensure a smooth and collaborative process.
-
Fork the repository to your GitHub account.
-
Clone the forked repository to your local machine:
git clone https://github.com/jaimin1618/nodejs-backend.git
- Clone the forked repository to your local machine:
git checkout -b feature/your-feature
- Make your changes, additions, or bug fixes.
- Commit your changes with a descriptive commit message:
git commit -m "Add feature: your-feature"
- Push your changes to your forked repository:
git push origin feature/your-feature
- Create a pull request (PR) from your branch to the main repository. Be sure to provide a clear and detailed description of your changes in the PR. Participate in the discussion and address any feedback or comments received on your PR.
If you have any feedback, please reach out to me at [email protected]