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

http-routify

v2.3.0

Published

Routify is a minimal http routing module for node.js.

Downloads

11

Readme

Routify NPM version

Routify is a minimal http routing module for node.

It mixes the simplicity of tiny-route with the parameter support of paramify.

Install

npm install http-routify

Example

var http = require('http')
var Stack = require('stack')
var router = require('http-routify')

http
  .createServer(
    Stack(
      router.get('/', (req, res) => res.end('Root')),
      router.post('/blog/posts/', (req, res) => {
        res.statusCode = 201
        res.end('Created blog post')
      }),
      router.get('/blog/posts/:slug', (req, res) => {
        res.end(`We support parameters as well: ${req.params.slug}`)
      })
    )
  )
  .listen(1337)

Running the example

The example uses Stack and requires node 8 or above.

npm start

Open up a browser or command line and test the routes on http://localhost:1337

Test

Running the tests requires node 8 or above.

npm test

Watch

npm run test:watch

API

Routing

router.verb(path,handler[,middleware])
  • path: <string>
    A path pattern relative to the root. Example: /api/users/:id

  • handler: <function>
    The handler to execute when the request matches. See Handlers.

  • middleware: <array>
    [Optional] Array of middleware to execute before executing the handler. Overrides all middleware globally applied to the router. See Middleware.

The router currently supports the following HTTP verbs

router.get

Matches HTTP GET requests

router.post

Matches HTTP POST requests

router.put

Matches HTTP PUT requests

router.delete

Matches HTTP DELETE requests

router.all

router.all is a bit special. It matches any HTTP verb and only matches the url against the path.

Handlers

Each route handler needs to be of the form:

function handler (req, res, next) {
  // Either handle the request here using `req` and `res`
  // or call `next()` to pass control to next route
}

The next function is only required in order to delegate handling of a request to the next route in the stack and may be omitted.

Middleware

The router supports defining middleware that wraps around each route handler, creating a chain of behaviors to be executed. Middleware can be defined globally and overridden on each route.

router.use

router.use(middleware)
  • middleware: <function>
    A function that wraps around all route handlers. Middleware may also be specified as an array of functions.

Creating middleware

A middleware function needs to be in the form of:

function middleware (inner) {
  return function (req, res, next) {
    inner(req, res, next)
  }
}

In general, a middleware should always call any inner functions (middleware or handler) passed to it. However, there are exceptions where you might want to skip calling inner behaviors and end the request in your middleware.

To run code after the inner behavior has executed you may use async/await in handlers and middleware. See example below.

Example middleware

router.use(inner => {
  return (req, res, next) => {
    console.log('Before handler')
    inner(req, res, next)
  }
})

router.use(inner => {
  return async (req, res, next) => {
    console.log('Before handler')
    await inner(req, res, next)
    console.log('After handler')
  }
})

More

Bugs and issues

Please report any bugs by opening an issues on Github.

Paramify

https://github.com/hij1nx/paramify

Stack

https://github.com/creationix/stack