Giter Site home page Giter Site logo

supra-api-nodejs's Introduction

Node.js API boilerplate

Piece of my thoughts about Node.js architecture.

supra-api-nodejs: A little bit about Node.js RESTful APIs Architecture (RU)

Highlights:

  • RESTful API
  • ES6 Classes
  • Action based
  • Service based
  • SQL based (PostgreSQL with objection.js)
  • Migrations(knex.js)
  • Auth (JWT/Access-token/Refresh-token)
  • Role based access control
  • Request validation (Joi.js)
  • CRUD(users, posts resources)
  • Automated API documentation

Key points:

0. Monolith first

Supra-api-nodejs its about monolith first approach.

1. Routing

Each entity have own router class that implement RESTful interface to work with it.

class PostsRouter extends BaseRouter {
  static get router () {
    router.get('/', this.actionRunner(actions.ListAction))
    router.get('/:id', this.actionRunner(actions.GetByIdAction))
    router.post('/', this.actionRunner(actions.CreateAction))
    router.patch('/:id', this.actionRunner(actions.UpdateAction))
    router.delete('/:id', this.actionRunner(actions.RemoveAction))

    return router
  }
}

For example PostsRouter implement base CRUD routes to post entity. Each route fires own action.

2. Action

It's a class that encapsulated request validation, permission verification and business logic. One file, one class, one REST operation.

3. Service

It's much more like utility layer thats provide some helpful promisfitated functions like check access, hash password or generate jwt's.

4. DAO

Implement data access methods.

Development:

Install global dependencies:

npm i -g knex nodemon

Setup database:

  1. Install PostgreSQL (https://postgresapp.com/downloads.html (for Mac OS))
  2. Create some DB (https://eggerapps.at/postico/ (for Mac OS))

Go ahead...

cd /supra-api-nodejs
  • cp .env.example .env
  • Set required credential in .env

Run migration to set base SQL schema

knex migrate:latest

Run server

npm run start // prod mode
npm run dev // dev mode

Implemented endpoints:

endpoints

!!! Project still in progress !!!

2017 - 2018 - 2019 ...

supra-api-nodejs's People

Contributors

zmts avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.