1.0.3 • Published 1 year ago
node-awesome-router v1.0.3
Developer-Friendly router for Nodejs servers
install
npm i -S node-awesome-router
Usage
Creating server
const http = require('http')
const app = require('express')()
const NodeAwesomeRouter = require('node-awesome-router')
Creating route
// POST -> DEV.site.com/myPath/authorize
const routesGroup1 = {
key: '/myPath',
routes: {
'/authorize' () {
return {
method: 'post',
schema: {
username: { type: 'string', min: 4, max: 15 },
password: { type: 'string', min: 4, max: 20 },
},
middleWares: [
(req, res, next) => { console.log('log1'); next();},
(req, res, next) => { console.log('log2'); next();},
],
async handler (req, res) {
// your logic here
return res.status(200).send('welcome')
}
}
}
},
subdomain: 'dev'
}
Another route
// GET -> USERS.site.com/user/info/1
const routesGroup2 = {
key: '/user',
routes: {
'/info/:id?' () {
return {
method: 'get',
middleWares: [
(req, res, next) => { console.log('auth attempt'); next()},
],
async handler (req, res) {
// your logic here
return res.status(200).send('welcome')
}
}
}
},
subdomain: 'users'
}
Global middlewares
const globalMiddleWare = (req, res, next) => {
console.log('another request to our beauty backend')
}
Setup router and start listen
const httpServer = http.createServer(app)
NodeAwesomeRouter({
app,
routes: [routesGroup1, routesGroup2, ],
middleWares: [globalMiddleWare, ],
on404 (req, res) {
return res.send('...')
}
})
httpServer.listen(8080, () => {
console.log('listening port 8080')
})
Syntax | Description |
---|---|
app | ExpressJS instance |
routes | array of routes |
middleWares | array of middleware functions |
on404 | 404 not found handler function |