0.18.0 • Published 1 year ago

generator-apirest v0.18.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

API rest generator

RESTful API generator using NodeJS, Express and Mongoose

Features

  • Highly customizable - You can choose what to install
  • Really RESTful - It follows the best practices
  • ES6! - Using babel
  • User registration API - Using passport (optional)
  • Social login API - Facebook, Google and GitHub (optional)
  • Password reset API - Sending emails with SendGrid API (optional)
  • Listing query strings - q, page, limit, fields etc. already provided by querymen
  • Query string validator - Using querymen
  • Request body validator - Using bodymen
  • Standard error responses - Using querymen and bodymen error handlers
  • Unit and integration tests - Using Jest
  • Continuous integration support - Using Travis CI
  • API docs generator - Using apidoc
  • Love ♥ - Using me

Installation

First, install Yeoman and generator-rest using npm (we assume you have pre-installed node.js).

npm install -g yo
npm install -g generator-rest

Generators

Then, you can use yo to generate your project.

yo rest # generate a new project
yo rest:api # generate a new api endpoint inside your project

Commands

After you generate your project, these commands are available in package.json.

npm run dev # run the API in development mode
npm run prod # run the API in production mode
npm run docs # generate API docs
npm run swagger # generate API swagger

Directory structure

Overview

You can customize the src and api directories.

src/
├─ api/
│  ├─ user/
│  │  ├─ controller.js
│  │  ├─ index.js
│  │  ├─ model.js
│  └─ index.js
├─ services/
│  ├─ express/
│  ├─ facebook/
│  ├─ mongoose/
│  ├─ passport/
│  ├─ sendgrid/
│  └─ your-service/
├─ app.js
├─ config.js
└─ index.js

src/api/

Here is where the API endpoints are defined. Each API has its own folder.

src/api/some-endpoint/model.js

It defines the Mongoose schema and model for the API endpoint. Any changes to the data model should be done here.

src/api/some-endpoint/controller.js

This is the API controller file. It defines the main router middlewares which use the API model.

src/api/some-endpoint/index.js

This is the entry file of the API. It defines the routes using, along other middlewares (like session, validation etc.), the middlewares defined in the some-endpoint.controller.js file.

services/

Here you can put helpers, libraries and other types of modules which you want to use in your APIs.