Demo: https://node-express-joi-starter.herokuapp.com/
A boilerplate for nodejs api based on express with Joi validator and swagger
There are a lot of boilerplates node api can be found today. But I wasn't be able to find boilerplate on express with validation and documentation at once. I prepared all in one starter. I hope this will help someone.
This is a boilerplate of REST API on Nodejs
based on Express
with Joi validator
and Swagger documentation.
Code based on ES6, async/await and as a consequence requires Node version >= 7.6.
For example was implemented CRUD for User model and authentication using JWT token. Also user session with JWT token stored in db.
The root of api is swagger ui based on swagger.json.
It can be made using npm run yaml2json
. Package yaml2json should be installed globally npm install -g yaml2json
.
# Clone the repository
git clone https://github.com/igormigunov/node-express-joi-starter.git
# Change directory
cd node-express-joi-starter
# Install dependencies
yarn install
# Start it in simple mode
npm start
# Start in dev mode using nodemoon
npm run dev
- folders
- controllers - Routes modules
- models - Mongoose models
- module - Helper modules for reuse in other project modules
- public - Static content
- tests - Tests
- files
- index.js - Application Entrypoint
- app.js - Main application file
- .env - Environment Variables
- .env.test - Environment Variables for tests
- Structure based on hackathon-starter
Mongoose models contains Joi validators and some plugins. Plugins:
For mongoose models should be used validate
object with keys
params
, body
or query
for validity appropriate request parts.
For each controllers should be defined validate
method which should be used
with celebrate middleware in app.js
.
At first should be corrected swagger.yaml file according to needed models.
After should do npm run yaml2json
and swagger.json
is ready
- MONGODB_URI - mongodb connection string
- JWT_SECRET - Your JWT secret
- SESSION_EXP - Session and JWT token lifetime in second
CRUD operations for user implemented in test. It contains before
method that
clear database before tests start. Just npm run test
to start it.
Test use .env.test environment variables file.
docker-compose up