This template is designed to kickstart your new projects with ease. It encapsulates fundamental concepts and provides a customizable foundation tailored to your project requirements.
- Quick setup for building RESTful APIs with Node.js, Express, and Mongoose.
- A production-ready Node.js application, installed and configured effortlessly.
- Built-in features include JWT-based authentication and request validation.
Stay tuned for updates by starring the repository!
- Make sure to setup the NodeJS Developer environment.
- Following are the pre-requisites:
-
Clone the repository
git clone https://github.com/saadjavaid67/nodejs-starter.git
-
Clone the repository
cd nodejs-starter
-
Install dependencies
npm install
- Rename/copy .env.example to '.env'
- Configure JWT_TOKEN_SECRET=
- Run the project
bash npm run start
This is how .env.example looks like:
PORT=5000
API_PREFIX='/api/v1'
JWT_TOKEN_SECRET=
SESSION_TIMEOUT="1h"
# DATABASE
MONGODB_URI='mongodb://localhost:27017/your_database'
- NoSQL database: MongoDB object data modeling using Mongoose
- Authentication and authorization: using JWT (access and refresh token)
- Validation: request data validation using Express Validator
- Environment variables: using dotenv
- CORS: Cross-Origin Resource-Sharing enabled using cors
Create a dedicated model file 'modelName.js' in models directory. You can simply copy the existing User.js to save time.
-
Establish a new route file 'modelNameRoutes.js' in routes directory. This file will handle incoming requests related to your model.
-
Incorporate the newly created route into the master routing file, /src/routes/index.js.
-
Example:
const modelNameRoutes = require('../api/routes/modelNameRoutes'); router.use('/modelName', modelNameRoutes);
If you need a validator in create 'modelNameValidator.js' in Validators directory. This validator will enforce data validation rules for your model. You can use userValidator.js as reference.
Usage Example: (in your 'modelNameRoutes.js' file)
const { registerValidator, loginValidator } = require("../validators/userValidator");
router.post("/register", registerValidator, userController.register);
Make a controller file 'modelNameController.js' in controllers directory. This file will contain the logic to process incoming requests and interact with the model. you can use userController.js as reference.
nodejs-starter
├─ confg
│ ├─ app.js
│ └─ database.js
├─ providers
│ ├─ AuthProvider.js
│ └─ DatabaseProvider.js
├─ src
│ ├─ api
│ │ ├─ middlewares
│ │ │ ├─ authMiddleware.js
│ │ │ └─ coreMiddleware.js
│ │ ├─ routes
│ │ │ ├─ index.js
│ │ │ └─ userRoutes.js
│ │ └─ validators
│ │ └─ userValidator.js
│ ├─ controllers
│ │ └─ userController.js
│ ├─ models
│ │ └─ User.js
│ └─ app.js
├─ .env.example
├─ .gitignore
├─ index.js
├─ LICENSE
├─ package-lock.json
├─ package.json
└─ README.md
You can use api.postman_collection.json as a API reference. List of available routes:
-
User Auth Routes:
- Register - POST /:PREFIX:/user
- Login - POST /:PREFIX:/user/login
- Logout - POST /:PREFIX:/user/logout
-
Other User Routes:
- Get All Users - GET /:PREFIX:/user
NOTE: PREFIX is defined in .env file. default is '/api/v1'.
Your contributions are invaluable, not just to me but to everyone seeking to benefit from this resource. I deeply appreciate your time and effort in making this project better.
If you have ideas, improvements, or bug fixes, don't hesitate to contribute. Every contribution, big or small, makes a significant impact.
How to Contribute:
- Fork the repository.
- Create a new branch for your changes.
- Make your improvements, additions, or fixes.
- Commit and push your changes to your fork.
- Submit a pull request.
Your contributions help build a vibrant and collaborative community. Thank you for being part of it! 🙌
THANK YOU!