0.0.6 • Published 3 years ago
nem-rest v0.0.6
RESTful API Express Mongodb Driver Boilerplate
Quick Start
To create a project, simply run:
npx nem-rest <app-name>
Or
npm init nem-rest <app-name>
Manual Installation
If you would still prefer to do the installation manually, follow these steps:
Clone the repo:
git clone https://github.com/Bartek-Figat/nem-rest-api
cd nem-rest
npx rimraf ./.git
Install the dependencies:
yarn install or npm install
Set the environment variables:
.env
# open .env and modify the environment variables (if needed)
Commands
Running locally:
yarn dev
npm run dev
Testing Command
npm run test
Application will run by default on port:8080
Authentication
const express = require('express');
const { Router } = express;
const { UserService } = require('../services/userService');
const { protectedRoutes } = require('../middleware/authentication');
const userRouter = Router();
userRouter.post('/login', async (req, res) => {
const credentials = req.body;
const response = await UserService.createToken(credentials);
try {
res.json({ response });
} catch (err) {
res.status(500);
}
});
module.exports = {
userRouter,
};
These routes require a valid JWT access token in the Authorization request header using the Bearer schema.
Authorization
const express = require('express');
const { Router } = express;
const { UserService } = require('../services/userService');
const { protectedRoutes } = require('../middleware/authentication');
const userRouter = Router();
userRouter.get('/detail', protectedRoutes, async (req, res) => {
const options = { projection: { _id: 0, password: 0 } };
const response = await UserService.showUser(req.user, options);
try {
res.json({ response });
} catch (err) {
res.status(500);
}
});
module.exports = {
userRouter,
};
Contributing
Contributions are more than welcome!