1.0.3 • Published 2 years ago

@dashdot/router v1.0.3

Weekly downloads
-
License
MIT
Repository
-
Last release
2 years ago

Dashdot Router

build codecov

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))),
))
1.0.3

2 years ago

1.0.2

3 years ago

1.0.0

3 years ago

0.8.0

3 years ago

0.7.0

3 years ago

0.6.0

3 years ago

0.5.0

4 years ago

0.4.4

4 years ago

0.4.1

4 years ago

0.4.0

4 years ago

0.4.3

4 years ago

0.3.0

4 years ago

0.2.0

4 years ago

0.1.0

4 years ago