@dashdot/router
v1.0.3
Published
A simple and functional native Node router with some helpers.
Downloads
309
Keywords
Readme
Dashdot Router
A simple and functional Node router with some helpers.
Usage
Inside your Node project directory, run the following:
npm i --save @dashdot/router
Or with Yarn:
yarn add @dashdot/router
API
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 http://${HOST}:${PORT}`)
})
Cross-Origin Resource Sharing
const cors = createCors({
allowMethods: ['GET'],
extendAllowHeaders: ['trace']
})
const server = createServer(createRouter(
get('/posts', (req, res) => cors(ok(res))),
))
Rate limiting
const rateLimit = createRateLimit({
window: 1000, // 1 sec
limit: 10, // 10 requests
})
const server = createServer(createRouter(
get('/posts', (req, res) => rateLimit(ok(res))),
))