Nanorouter is an incredibly lightweight routing library for Node.js.
It provides a simple and efficient way to define and handle HTTP requests based on their method and path. This library is designed for minimal overhead and maximum flexibility, making it suitable for small to medium-sized projects where a full-fledged framework might be overkill.
-
Installation:
npm install nanorouter
-
Usage: Import the
router
androute
functions fromnanorouter
.
NanoRouter does not require any external configuration.
This example demonstrates how to define routes and handle requests using NanoRouter:
import { router, route } from 'nanorouter'
router.get('/hello', (req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' })
res.end('Hello, GET!')
})
router.post('/world', (req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' })
res.end('Hello, POST!')
})
router.any('/any', (req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' })
res.end(`Hello, ${req.method}!`)
})
//Example with next function
router.get('/chained', (req, res, next) => {
console.log("First handler")
next()
})
router.get('/chained', (req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' })
res.end('Chained Handler')
})
const http = require('http')
const server = http.createServer(route)
await new Promise(res => server.listen(3000, res))
console.log('Server listening on port 3000')