0.3.0 • Published 2 years ago

mlwn-derouter v0.3.0

Weekly downloads
-
License
BSD-2-Clause
Repository
-
Last release
2 years ago

npm

derouter

Declaratory router for express js

init path

  • depends on node env variable (process.env.INIT_CWD):
INIT_CWD
  • it should be set to root of project

index.js file of app

// Load modules
import express from 'express'
import derouter from '../../main.mjs'

const routerDeclaration = {
  prefix: '/hello',
  middleware: [
    (req, res, next) => {
      console.log('----hello middleware---')

      /**
       * Necessary to call function next() (Express middleware)
       */
      next()
    }
  ],
  routes: {
    GET: {
      '/:name': [
        'example.basic.action.today->date',
        'example.basic.action.greet:#Greetings:&.date:params.name'
      ]
    }
  }
}

const app = express()

async function init() {
  // load routes async
  const routes = await derouter(routerDeclaration)

  app.use(routes)

  const port = 8083
  app.listen(port, () => {
    console.log(`App listening on port ${port}`)
  })  
}

init()