0.7.8 • Published 11 months ago

wroute v0.7.8

Weekly downloads
-
License
MIT
Repository
-
Last release
11 months ago

LICENSE npm downloads bundle size

wroute

Minimal RFC6570 conforming HTTP multiplexer for defining endpoints with typed URI Template Syntax

pnpm add wroute

features

  • RFC6570
  • Typed URL & Query Parameters
  • No Dependencies

example

define routes

router.ts
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)
server.ts
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`
  ))
:rocket: launch the server
» 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

status codes

  • 404 status when request is to a missing endpoint
  • 405 status when request is to a missing method
200
» curl -s 'http://0.0.0.0:3000' | yq .content
Hello World!
404
» curl -sI 'http://0.0.0.0:3000/nope'
HTTP/1.1 404 Not Found
Content-Type: text/plain
...
405
» curl -sI 'http://0.0.0.0:3000'
HTTP/1.1 405 Method Not Allowed
Content-Type: text/plain
...

0.7.8

11 months ago

0.7.7

11 months ago

0.7.6

12 months ago

0.7.5

12 months ago

0.7.0

12 months ago

0.6.0

12 months ago

0.5.1

12 months ago

0.4.0

12 months ago

0.3.0

12 months ago

0.2.2

12 months ago

0.2.1

12 months ago

0.2.0

12 months ago