BackendStack21 / restana

Restana is a lightweight and fast Node.js framework for building RESTful APIs.
MIT License
467 stars 27 forks source link

Modular, mountable route handlers #86

Closed deman4ik closed 4 years ago

deman4ik commented 4 years ago

Is there a way to create modular router without creating 'service' instance and calling 'service.newRouter()' first?

Like with express.Router

var express = require('express')
var router = express.Router()

// middleware that is specific to this router
router.use(function timeLog (req, res, next) {
  console.log('Time: ', Date.now())
  next()
})
// define the home page route
router.get('/', function (req, res) {
  res.send('Birds home page')
})
// define the about route
router.get('/about', function (req, res) {
  res.send('About birds')
})

module.exports = router
var birds = require('./birds')

// ...

app.use('/birds', birds)
jkyberneees commented 4 years ago

Hi @deman4ik , thanks for getting in contact.

Please see an example below:

'use strict'

// example on how to setup modular routers
// https://github.com/jkyberneees/0http#0http---sequential-default-router
const sequential = require('0http/lib/router/sequential')
const router1 = sequential()
router1.get('/hi/:name', (req, res) => {
  res.send('Hello ' + req.params.name)
})

// restana service
const service = require('./../index')({})
service.use('/actions', router1)

service.start()

Then

curl http://localhost:3000/actions/hi/Bill

The major difference from this method to the newRouter method in the service instance is the downstream referencing to the errorHandler function.

deman4ik commented 4 years ago

Great! Thank you