2.4.0 • Published 2 years ago

tiny-http-router v2.4.0

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

tiny-http-router

A super tiny router for node http server.

Install

npm install --save tiny-http-router

Use

import http from 'http'
import { router, method, methods } from 'tiny-http-router'

const yolo = async (req, res, { send, id }) => {
  send(200, id || 'yolo')
}

const server = http.createServer(router({
  // Routes
  '/yolo' : yolo,
  '/yolo/:id' : method('GET', yolo),
  '/bolo/:id' : methods({
    GET: yolo,
    POST: yolo
  })
}, (req, res, { send }) => {
  // Default (if not route hits)
  send(200, 'default')
})).listen(8080)

Exports

router  - funciton that takes object of routes
method  - function wrapper to enforce http method
methods - function wrapper to enforce http methods

Params

Params is an object passed as the 3rd parameter to any function used as a route. It has the following properties.

send - helper function to easily respond to http request
query - query params
json - async helper function to parse json payload
text - async helper function to parse text payload

enjoy.

2.4.0

2 years ago

2.3.0

2 years ago

2.2.0

2 years ago

2.1.0

2 years ago

2.0.0

2 years ago

1.1.0

2 years ago

1.0.0

2 years ago