Simple NodeJS server with express providing basic authentification with mongodb
run it:
docker-compose up
or yarn start-docker
dev with hot reload on sources:
docker-compose -f docker-compose.yml -f docker-compose.dev.yml up
alias on yarn dev
Start mongodb.
node src/server.js
don't forget to setup the environment
Here is multiple environment variable that need to be setup (default value in docker-compose.yml):
- CORS_WHITELIST
- MONGODB_ENDPOINT
- MONGODB_USERNAME
- MONGODB_PASSWORD
- DOMAIN
Route: /login
Method: POST
body:
{
mail: "[email protected]"
password: "licorne"
}
returns:
{
error: "",
mail: "[email protected]",
_id: "38984a3"
}
Route: /register
Method: POST
body:
{
mail: "[email protected]"
password: "licorne"
}
returns:
{
error: “”
}
Route: /logout
Method: POST
Route: /changepassword
Method: POST
body:
{
lastPassword: "licorne"
newPassword: "fromage"
}
returns:
{
error: "",
}
Route: /health
Method GET
Return 200
if the server is in an healthy state. Basicly if the server is connected to the database.
You can easily check if the user is logged using the simple logged middelware.
const { logged } = require('./middleware');
routes.get('/me', logged, (req, res) => {
return res.status(200).send(req.user);
});
User profile is autaumatically injected (even without the middleware). You can access it using req.user
.
Tests are in tests
dir
You can easly setup you own tests to call your logged routes
Template is in tests/template.js
.
Authentification is done through cookies.
getCookies function generate cookies automatically
before(async () => {
cookies = await getCookies(request);
});
You can give mail and password as 2nd and 3rd parameter
To be logged you just need to set the cookies like this
request
.set('Cookie', cookies)
Server logs are hide if test succeed. It allow you to debug easly and faster your project.
Include:
const { beforeEachLog, afterEachLog } = require('./logs');
Add in your test battery:
beforeEach(function() {
beforeEachLog();
});
afterEach(function() {
afterEachLog(this.currentTest.state);
});
Run yarn test
to run all tests. It will automatically reset the database.
Docker is required.
You can use your own script to reset the database and run mocha without docker !
ESLint is an open source project, its goal is to provide a pluggable linting utility for JavaScript.
On this project airbnb's eslint configuration is used
Run yarn lint
to have a linting report.
A winston logger is present. It provide easly log manipulation / filtering and more flexibility like hiding logs during test.
- Error log are present in
error.log
- Console like log are present in
console.log
- Debug log are present in
debug.log
This template is far of perfect. If you have any idea or enchancment, don't hesitate to contribute.