node.js boilerplate to make bulding RESTful API faster, with mongodb nosql database.
https://github.com/x249/node-api
"description": "Backend API built using TypeScript, Express and MongoDB"
- User model with Authentication
- Easy Validation Process
- Json web token
- Use ES6 features
Clone the project
git clone https://github.com/Tayeb-Ali/RestApi.git
Install dependencies
npm install
or
yarn install
npm start
you can put the port number, mongodb connection link, and json web token key from .env
:
PORT = 5000
DATABASE = connection_link
JWT_KEY = key
you can put your routes and assign them to controllers in index.js
inside routes folder
router.get('/users/me', asyncMethod(isLogggedIn), userController.getData);
I used express-validator package for validation.
put your validation rules inside validators.js
middleware inside middleware folder and use you rule like this:
router.post('/users/register',validate(SaveUser) ,asyncMethod(userController.register));
when using async/await in controller method put the method inside asyncMethod from index.js
inside helpers folder to catch Promises errors.
router.post('/users/login', asyncMethod(userController.login));
you can gurd any routes using auth middleware like this:
router.get('/users/me', asyncMethod(isLogggedIn), userController.getData);
- Route: /users/getData
- Method: GET
- Authorization: token
- Result: current user data