A simple and functional Node router with some helpers.
Inside your Node project directory, run the following:
npm i --save @dashdot/router
Or with Yarn:
yarn add @dashdot/router
import { createServer } from 'http'
import { createRouter, get, post, put, del, all, ok, notFound } from '@dashdot/router'
const { PORT, HOST } = process.env
const server = createServer(createRouter(
get('/posts/:id', (req, res) => ok(res, req.params.id)),
post('/posts/:id', (req, res) => ok(res, req.params.id)),
put('/posts/:id', (req, res) => ok(res, req.params.id)),
del('/posts/:id', (req, res) => ok(res, req.params.id)),
all('/*', (req, res) => notFound(res)),
))
server.listen(PORT, HOST, () => {
console.log(`Server started and listening on https://${HOST}:${PORT}`)
})
const cors = createCors({
allowMethods: ['GET'],
extendAllowHeaders: ['trace']
})
const server = createServer(createRouter(
get('/posts', (req, res) => cors(ok(res))),
))
const rateLimit = createRateLimit({
window: 1000, // 1 sec
limit: 10, // 10 requests
})
const server = createServer(createRouter(
get('/posts', (req, res) => rateLimit(ok(res))),
))