0.0.2 • Published 6 years ago

express-import-machine v0.0.2

Weekly downloads
-
License
ISC
Repository
-
Last release
6 years ago

express import machine

The task of this module is transferring responsibility for computing routes to individual files.

creating router

Router is created from js files and folders in specified directory.

api
└─ user
   ├─ login.js
   ├─ register.js
   └─ profile.js

utils
├─ db.js
└─ jwt.js

app.js

const express = require('express')
const app = express()
const ims = require('express-import-machine')

ims.use('utils')
app.use('/api', ims.createRouter('api'))

login.js

module.exports = {
  method: 'POST',
  respond: (req, res) => {
    ...
  }
}

middleware

1) add middleware 2) use middleware by adding curring to func

module.exports = {
  method: 'POST',
  respond: jwt => (req, res) => {
    ...
  }
}

shared modules

Shared modules alows you to setup module once and use it in all routes without extra import line.

module.exports = {
  method: 'POST',
  respond: jwt => (req, res, { db }) => {
    ...
  },
  init: ({ db }) => {
    ...
  }
}