/node-express-mongoose

ES6 - Node, express and mongoose boilerplate.

Primary LanguageJavaScriptMIT LicenseMIT

Node, Express & Mongoose REST API Boilerplate in ES6

Express ES6 REST API Starter

Overview

This is a boilerplate application for building REST APIs in Node.js using ES6 and Express with Code Coverage and JWT Authentication. Helps you stay productive by following best practices. Follows Airbnb's Javascript style guide.

Heavily inspired from Express & mongoose REST API Boilerplate in ES6 with Code Coverage Awesome with my own improvements.

Features

Feature Summary
Authentication via JsonWebToken Supports authentication using jsonwebtoken.
Code Linting JavaScript code linting is done using ESLint - a pluggable linter tool for identifying and reporting on patterns in JavaScript. Uses ESLint with eslint-config-airbnb, which tries to follow the Airbnb JavaScript style guide.
Auto server restart Restart the server using nodemon in real-time anytime an edit is made, with Gulp and eslint.
ES6 Code Coverage via istanbul Supports code coverage of ES6 code using istanbul and mocha. Code coverage reports are saved in coverage/ directory post yarn test execution. Open coverage/lcov-report/index.html to view coverage report. yarn test also displays code coverage summary on console. Code coverage can also be enforced overall and per file as well, configured via .istanbul.yml
API parameter validation via Joi Validate body, params, query, headers and cookies of a request (via middleware) and return a response with errors; if any of the configured validation rules fail.
Secure app via helmet Helmet helps secure Express apps by setting various HTTP headers.
Uses yarn over npm Uses new released yarn package manager by facebook. You can read more about it here
  • CORS support via cors
  • Uses http-status to set http status code. It is recommended to use httpStatus.INTERNAL_SERVER_ERROR instead of directly using 500 when setting status code.
  • Has .editorconfig which helps developers define and maintain consistent coding styles between different editors and IDEs.

Getting Started

Clone the repo:

git clone https://github.com/Ellebkey/node-express-mongoose.git
cd node-express-mongoose

Install yarn:

npm install -g yarn

Install dependencies:

yarn

Set environment (vars):

cp .env.example .env

Start server:

# Start server
yarn start

# or user gulp to start via debug and auto reload
gulp

Tests:

# Run tests written in ES6 
yarn test

# Run test along with code coverage
yarn test:coverage

# Run tests on file change
yarn test:watch

# Run tests enforcing code coverage (configured via .istanbul.yml)
yarn test:check-coverage

Lint:

# Lint code with ESLint
yarn lint

# Run lint on any file change
yarn lint:watch
Deployment
# compile to ES5
1. yarn build

# upload dist/ to your server
2. scp -rp dist/ user@dest:/path

# install production dependencies only
3. yarn --production

# Use any process manager to start your services
4. pm2 start dist/index.js

In production you need to make sure your server is always up so you should ideally use any of the process manager recommended here. We recommend pm2 as it has several useful features like it can be configured to auto-start your services if system is rebooted.

Docker

Using Docker Compose for Development

# service restarts on file change
bash bin/development.sh

Building and running without Docker Compose

# To use this option you need to make sure mongodb is listening on port 27017

# Build docker 
docker build -t express-mongoose-es6-rest-api .

# Run docker
docker run -p 4040:4040 express-mongoose-es6-rest-api

A Boilerplate-only Option

If you would prefer not to use any of our tooling, delete the following files from the project: package.json, gulpFile.js, .eslintrc and .travis.yml. You can now safely use the boilerplate with an alternative build-system or no build-system at all if you choose.

Docs and Recipes

  • Gulp recipes - the official Gulp recipes directory includes a comprehensive list of guides for different workflows you can add to your project.

Contributing

Contributions, questions and comments are all welcome and encouraged. For code contributions submit a pull request with unit test.

License

This project is licensed under the MIT License

Meta

Joel Barranco – @Ellebkeyhello@joelbarranco.io