Skip to content

aicirt2012/icm-server

Repository files navigation

Generate API Doc

Install apidoc globally by running

npm install apidoc -g

then run

apidoc -i server/controllers/ -o apidoc/generated -t apidoc/template

Intelligent Contextual Mail Server

Based on Express & mongoose REST API Boilerplate in ES6 with Code Coverage.

Overview

The underlying project follows Airbnb's Javascript style guide.

Getting Started

Setup

Clone the repo:

git clone git@bitbucket.org:aicirt2012/icm-server.git
cd icm-server

Install dependencies:

If you're having problems installing on windows, see the troubleshooting at the end of this file.

npm install

Start MongoDB:

# Start mongodb (e.g.)
mongod --dbpath ~/workspace/mongodb/data/

Start server:

# Start server (default Port 4000)
npm start

# Selectively set DEBUG env var to get logs
DEBUG=icm-server:* npm start

Refer debug to know how to selectively turn on logs.

Config

EWSConnector

The SyncFolderItems operation will return a maximum of 512 changes (for all boxes). Subsequent SyncFolderItems requests must be performed to get additional changes. Refer to the constant MAX_CHANGES_RETURNED in EWSConnector.js

Tests

# Run tests written in ES6 along with code coverage
npm test

# Run tests on file change
npm run test:watch

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

ES6 Lint

# Lint code with ESLint
npm run lint

# Run lint on any file change
npm run lint:watch

Other gulp tasks

# Wipe out dist and coverage directory
gulp clean

# Default task: Wipes out dist and coverage directory. Compiles using babel.
gulp
Deployment
# compile to ES5
1. npm run build

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

# install production dependencies only
3. npm i --production

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

# OR: for a normal node execution in production environment
4. set node_env=production&& node dist/index.js 

Features

Feature Summary
ES6 via Babel ES6 support using Babel.
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 babel compilation 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 npm test execution. Open coverage/lcov-report/index.html to view coverage report. npm test also displays code coverage summary on console. Code coverage can also be enforced overall and per file as well, configured via .istanbul.yml
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.
Promisified Code via bluebird We love promise, don't we ? All our code is promisified and even so our tests via supertest-as-promised.
API parameter validation via express-validation 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. You won't anymore need to make your route handler dirty with such validations.
Pre-commit hooks Runs lint and tests before any commit is made locally, making sure that only tested and quality code is committed
Secure app via helmet Helmet helps secure Express apps by setting various HTTP headers.
  • 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.

More Info

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.

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. For example, one may want error logs to be stored in a persistent remote location (like a database), but all logs output to the console or a local file. We just log to the console for simplicity, you can configure more transports as per your requirement.

API logging

Logs detailed info about each api request to console during development. Detailed API logging

Error logging

Logs stacktrace of error to console along with other details. You should ideally store all error messages persistently. Error logging

Code Coverage

Get code coverage summary on executing npm test Code Coverage Text Summary

npm test also generates HTML code coverage report in coverage/ directory. Open lcov-report/index.html to view it. Code coverage HTML report

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.

Common issues

node-gyp requiring python during npm install on windows

To fix this, simply run:

npm install --global --production windows-build-tools

http://www.westerndevs.com/JavaScript/How-to-Fix-node-gyp-Error-on-Windows/

https://github.com/nodejs/node-gyp

Configuring debugging with breakpoints in WebStorm

To use breakpoints in WebStorm, a separate run configuration needs to be created.

  1. Create new NodeJS run configuration

1 2. Configure like in the screenshot below

2 3. Select the run configuration and start the server using the debug button

3