JSPM

  • Created
  • Published
  • Downloads 37
  • Score
    100M100P100Q63677F
  • License MIT

Simple and fast Restful API designed for horizontal scalability including cluster, based on Swagger, Socket.io, Nodejs, Redis + Passport, MongoDB, Express

Package Exports

  • nodetomic-api-swagger

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (nodetomic-api-swagger) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Nodetomic-api-swagger

Codacy Badge NPM version Build Status dependencies Status devDependencies Status Gitter chat GitHub license Downloads

Simple and fast Restful API designed for horizontal scalability including cluster, based on Swagger, Socket.io, Nodejs, Redis + Passport, MongoDB, Express

Horizontal scalability

Preview

Swagger Api

http://localhost:8000/docs

Technologies

Codacy

Features

  • Tokens
    • JWT merged with REDIS
    • Expiration time
  • Redis
    • Pub/Sub
    • Events: Expired, del, etc
  • Middleware
    • Login validation
    • Role validation
  • Socket.io
    • Support cluster
    • Socket Redis
  • Authentication
    • local
    • facebook
    • twitter
    • google
    • github
    • bitbucket
    • and more...
  • Session
    • Single or Multiple (One or more devices at the same time)
    • Get list of sessions
    • Destroy a session
  • Node
    • Support cluster
    • Support Pm2
  • Swagger
    • Routers by swagger
    • Swagger UI

Requirements

Installation

git clone https://github.com/kevoj/nodetomic-api-swagger
cd nodetomic-api-swagger
npm i

How to create..

Model

src/api/models/hello.js

import mongoose from 'mongoose';
const Schema = mongoose.Schema;

const HelloSchema = new Schema({
  greet: {
    type: String,
    required: [true, 'Greet is required.']
  },
  language: {
    type: String,
    required: [true, 'Language is required.']
  }
});

export default mongoose.model('Hello', HelloSchema);

Controller

src/api/controllers/hello.js

import { result, notFound, error } from 'express-easy-helper';
import Hello from '../models/hello';

export function list(req, res) {
    return Hello.find().exec()
    .then(notFound(res))
    .then(result(res))
    .catch(error(res));
}

Swagger (Router)

src/api/swagger/hello.yaml


/api/hello:
  x-swagger-router-controller: hello 
  get:
    operationId: list
    tags:
      - Hello
    summary: Get list Hello's
    description: Returns all hello
    responses:
      200:
        description: Success
      404:
        description: Not found
      500:
        description: Error

Swagger (Router + middleware)

src/api/swagger/hello.yaml


/api/hello:
  x-swagger-router-controller: hello 
  get:
    operationId: list
    security:
      - Bearer: []
    x-security-scopes:
      - admin
    tags:
      - Hello
    summary: Get list Hello's
    description: Returns all hello
    responses:
      200:
        description: Success
      404:
        description: Not found
      500:
        description: Error

Socket

src/api/sockets/hello.js

let socket = null;
let io = null;

// Constructor
export default (_socket, _io) => {
    socket = _socket;
    io = _io;
    on();
}

// Here should be all events 'on'
export function on() {
    // Listen 'example'
    socket.on('example', function (data) {
        // emit to cool
        io.emit('cool', data);
    });
}

export function emit(data) {
    io.emit('other event', data);
}

Controller + Socket

src/api/controllers/hello.js

import { result, notFound, error } from 'express-easy-helper';
import { emit as socket } from '../sockets/hello';

export function list(req, res) {

    socket.emit('hello world');
  return result(res, 'Emited!');

}

Development

Start

npm start

Imgur

Build

npm run build

Imgur

  • Generate output folder: dist

Imgur

Test

npm test

Imgur

Lint

npm lint

Pm2 [Development]

Simple

Run pm2 in a single thread and run the monitor

npm run dev-simple

Imgur

Cluster

Run pm2 in multiple threads and run the monitor

npm run dev-cluster

Imgur

Pm2 [Production]

Simple

Run pm2 in a single thread

npm run simple

Imgur

Cluster

Run pm2 in multiple threads

npm run cluster

Imgur

Stop

PM2

destroy pm2 simple and pm2 cluster

npm stop

Node

destroyed all process for node

killall node

License

MIT © Leonardo Rico