Minimal RFC6570 conforming HTTP multiplexer for defining endpoints with typed URI Template Syntax
pnpm add wroute
- RFC6570
- Typed URL & Query Parameters
- No Dependencies
import { IncomingMessage as Request, ServerResponse as Response } from 'node:http'
import { router as wroute } from 'wroute'
const routes = {
'/': {
GET: async (req: Request, res: Response) => {
res.writeHead(200, { 'Content-Type': 'application/json' })
res.end(JSON.stringify({ content: 'Hello World!' }))
},
},
}
export const router = () => wroute(routes)
import { createServer } from 'node:http'
import { router } from './router'
const port = Number(process.env.PORT || 3000)
const host = String(process.env.HOST || `0.0.0.0`)
createServer(router())
.listen(port, host, () => console.log(
`🚀 Server is running! | Listening @ http://${host}:${port}. | To stop the server, press CTRL+C`
))
» pnpm i
» touch .env
» node --watch --env-file=.env -r ts-node/register server
🚀 Server is running! | Listening on http://0.0.0.0:3000. | To stop the server, press CTRL+C
» curl -s 'http://0.0.0.0:3000' | yq .content
Hello World!
» curl -sI 'http://0.0.0.0:3000/nope'
HTTP/1.1 404 Not Found
Content-Type: text/plain
...
» curl -sI 'http://0.0.0.0:3000'
HTTP/1.1 405 Method Not Allowed
Content-Type: text/plain
...