Skip to content

hjfitz/nanorouter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

nanorouter: minimal routing, maximum performance

Purpose

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.

Getting Started

  1. Installation:

    npm install nanorouter
  2. Usage: Import the router and route functions from nanorouter.

Configuration

NanoRouter does not require any external configuration.

Usage

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')

About

route but smol

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published