This is a boilerplate application for building RESTful APIs Microservice in Node.js using express and mongo DB mongoose in ES6 with code coverage and other developer tools
ESM - Most of the commonly used ES6 features are supported by Node version 10.15.3, ES6 module loading specifications are not yet a standard in the node version. ESM is a module loader that transforms es modules at run time instead of transpiling them.(Typically we use babel for command line transpiling ES6 code to ES5)
Other option is to use Babel
Babel-cli: Compile files from the command line using babel
Babel-preset-es2015: Babel preset for all es2015 plugins.
Alert: Seeing some issue with Mocha when it is used without babel, this needs to be investigated
Code Formatting - Code formatting is done using Prettier
Pre-commit hooks - Husky Ensures the code is formatted as per the development standard for every commit
Istanbul instruments your ES5 and ES2015+ JavaScript code with line counters, so that we can track how well your unit-tests exercise your codebase.The nyc command-line-client for Istanbul works well with most of the javascript frameworks.
jsdoc has been used for API documentation To set this locally install jsdoc globally or as a dev dependency as part of the setup
npm install -g jsdoc
Run the following command to generate documentation for the code base
yarn build-docs
Run the following command to generate documentation for a single file (yourJavaScriptFile)
jsdoc yourJavaScriptFile.js
Chai and Mocha are used for writing unit test cases
Assertion with Chai provides natural language assertions, expressive and readable style.
Restart the server using nodemon in real-time anytime an edit is made.
Debugging Via Debug
Instead of inserting and deleting console.log you can replace it with the debug function and just leave it there. You can then selectively debug portions of your code by setting DEBUG env variable. If DEBUG env variable is not set, nothing is displayed to the console
Feature | Summary |
---|---|
API Parameter validation | Schema based parameter validation is done using @hapi/joi |
Security Via Helmet | Helmet helps secure Express apps by setting various HTTP headers. |
CORs support Via cors | middleware that can be used to enable CORS with various options. |
HTTP Status Codes | 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. |
Env variables | Uses dotenv to load environment variables from a .env file into process.env . Storing configuration in the environment separate from code |
Logging | Universal logging library winston is used for logging. It has support for multiple transports. A transport is essentially a storage device for your logs. Each instance of a winston logger can have multiple transports configured at different levels |
Compression | Uses the compression middleware to compress responses. deflate & gzip are supported |
Http Errors | Uses http-errors to create HTTP Errors |
Mongoose | Uses Mongoose for ODM ,Schema-based solution to model your application data. It includes built-in type casting, validation, query building, business logic hooks and more, out of the box. |
Pre requisites
- yarn
- mongo
yarn
NODE_ENV=development PORT=5000 JWT_SECRET=0a6b944d-d2fb-46fc-a85e-0295c986cd9f MONGO_HOST=mongodb://localhost/express-mongo-api MONGO_PORT=270171
cp config/env/.env.production .env
yarn start
Run the test from command line using
yarn test
If uou are using vs code you can use either 'Test - Mocha All or Test - Mocha Current File' to run and debug the test cases.
yarn coverage
cp .env.production .env
Use the following command to install the only the production dependencies
yarn --production
In production we need to make sure your server is always up so you should ideally use any of the process manager recommended here. pm2 is recommended as it has several useful features like it can be configured to auto-start your services if system is rebooted.
pm2 start bin/www