npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@iopa/router

v4.0.19

Published

Lightweight and fast router for IOPA applications

Downloads

15

Readme

IOPA @iopa/router

NPM

NPM

About

@iopa/router is lightweight and fast router for the IOPA framework

It routes HTTP, COAP and MQTT requests, with simple uri templates and parameter/query string parsing.

Usage

Installation:

npm install @iopa/router

Hello World

import type { IContextIopa, IRouterApp, Next} from '@iopa/types'
import { RouterApp } from 'iopa'
import { RouterMiddleware }  from '@iopa/router'

const app: IRouterApp = new RouterApp()

app
  .use(FlipperMiddleware, 'Flipper Middleware')
  .use(RouterMiddleware, 'Router Middleware')
  .get('/hello', async function (context: IContextIopa, next: Next) {
    return context.response.html('<HTML><HEAD></HEAD><BODY>Hello World</BODY>')
  })
  .get('/goodbye', async function (context: IContextIopa, next: Next) {
    return context.response.html('<HTML><HEAD></HEAD><BODY>Goodbye World</BODY>')
  })

Methods

  • app.get: Match method GET requests
  • app.post: Match method POST requests
  • app.put: Match method PUT requests
  • app.head: Match method HEAD requests
  • app.del: Match method DELETE requests
  • app.options: Match method OPTIONS requests
  • app.all: Match all above request methods

API

If you want to grab a part of the path you can use capture groups in the pattern:

route.get('/id/:customer', function(context) {
	var customer = context.params.customer; // ex: if the path is /id/bar, then customer = bar

Query paramaters in the url are also added to context.params:

route.get('/id/:customer', function(context) {
  var base = context.params.base // ex: if the path is /id/bar?name=dog, then customer = bar
  var name = context.params.name // ex: if the path is /id/bar?name=dog, then name = dog
})

Routing

Basic

// HTTP Methods
app.get('/', (c) => c.response.text('GET /'))
app.post('/', (c) => c.response.text('POST /'))
app.put('/', (c) => c.response.text('PUT /'))
app.delete('/', (c) => c.response.text('DELETE /'))

// Wildcard
app.get('/wild/*/card', (c) => {
  return c.response.text('GET /wild/*/card')
})

// Any HTTP methods
app.all('/hello', (c) => c.response.text('Any Method /hello'))

Named Parameter

app.get('/user/:name', (c) => {
  const name = c.param('name')
  ...
})

or all parameters at once:

app.get('/posts/:id/comment/:comment_id', (c) => {
  const { id, comment_id } = c.param()
  ...
})

Regexp

app.get('/post/:date{[0-9]+}/:title{[a-z]+}', (c) => {
  const { date, title } = c.req.param()
  ...
})

Chained route

app
  .get('/endpoint', (c) => {
    return c.response.text('GET /endpoint')
  })
  .post((c) => {
    return c.response.text('POST /endpoint')
  })
  .delete((c) => {
    return c.response.text('DELETE /endpoint')
  })

Not Found Handler (use default App in IOPA)

function NotFound(context: IContextIopa): HandlerResult {
  return context.respondWith({msg: 'NOT FOUND'}, 404)
}
NotFound.id = 'NotFound'
app.properties.set('server.NotFound', NotFound)

Prior Art

For V4, this router was ported from honojs/hono which was developed under MIT license by Yusuke Wada, and enhanced and simplified to fit the IOPA architecture. Essentially the very fast Trie Router and RegExp Router are provided by hono unchanged, with most of the pipeline composition handled by IOPA

It has been developed to be a core component of the IOPA ecosystem.

License

MIT