Skip to content

Quick object-oriented implementation of express to help you get started on express development

Notifications You must be signed in to change notification settings

Pkfication/vanilla-express

Repository files navigation

Vanilla Express

An Object Oriented express server, talking to MONGODB via mongoose, intercepted by redis caching middleware, bundled with a generic CRUD class which takes care of all new mongoose model's CRUD without writing CRUD operations. Beautifully designed controllers, which solves all the code repeatation problems. Beautifully designed Caches which handles all the operations asynchronously and centrally.

A Perfect Start up kit for any kind of Express API application.

Getting Started

Before getting started, make sure you've the latest version of following:

  • node
  • redis
  • mongodb

Manual Installation

git clone [email protected]:Pkfication/vanilla-express.git
cd vanilla-express

Install the dependencies

npm install

Copy configs

cp .config_dev.json .config.json

Table of Content

Features

  • ES9: latest ECMAScript features
  • NoSQL database: MongoDB object data modeling using Mongoose
  • Validation: request data validation using Joi
  • Logging: using winston and morgan
  • Testing: unit and integration tests using Jest
  • Error handling: Object Oriented Error Handling, Very easy to extend custom errors
  • Process management: advanced production process management using PM2
  • Code coverage: using coveralls
  • Code quality: with Codacy
  • Git hooks: with husky and lint-staged
  • Linting: with ESLint and Prettier

Commands

Running locally:

npm start

Running in production:

npm prod

Testing:

# run all tests
npm test


# run test coverage
npm coverage

Project Structure

 |--config\         # Environment variables and configuration related things
 |--controllers\    # Route controllers (controller layer)
 |--caches\         # Custom redis cache classes
 |--models\         # Mongoose models (data layer)
 |--routes\         # Routes
 |--services\       # Business logic (service layer)
 |--helper\         # Utility classes and functions
 |--app.js          # Express app
 |--bin/www         # App entry point

Error Handling

The app has a centralized error handling mechanism.

Controllers should try to catch the errors and forward them to the error handling middleware (by calling next(error)). For convenience, you can also wrap the controller inside the catchAsync utility wrapper, which forwards the error.

// Custom Not Found Error
class NotFoundError extends Error {
  constructor(message = 'Not Found') {
    super();
    this.message = message;
    this.statusCode = 404;
  }
}

module.exports = NotFoundError;

// Picks up the status codes and messages automatically
const resource = await this.model.findById(req.params.id)
      .orFail(new NotFoundError());
{
    "status": "error",
    "statusCode": 404,
    "message": "Not Found"
}

This Custom Error Handlers are available globally and can be used from anywhere within the project.

Validation

Request data is validated using Joi. Check the documentation for more details on how to write Joi validation schemas.

The validation schemas are defined in the constrollers directory and are used in the routes by providing them as parameters to the validate middleware.

Linting

Linting is done using ESLint and Prettier.

In this app, ESLint is configured to follow the Airbnb JavaScript style guide with some modifications. It also extends eslint-config-prettier to turn off all rules that are unnecessary or might conflict with Prettier.

To modify the ESLint configuration, update the .eslintrc.json file. To modify the Prettier configuration, update the .prettierrc.json file.

To prevent a certain file or directory from being linted, add it to .eslintignore and .prettierignore.

To maintain a consistent coding style across different IDEs, the project contains .editorconfig