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

router-middleware

v4.0.5

Published

Super lightweight express style router

Downloads

116

Readme

Write fully featured http services and streaming templates without the bloat

Build Status

Coverage Status

router-middleware

Full Example

var http = require("http");
var router = require("router-middleware");
var app = router();
var server = http.createServer(app);

app.post("/user/:userId/email", router.bodyParser, function (req, res, next) {
  console.log("Query:", req.query);
  // { authToken: '1234' }
  console.log("Params:", req.params);
  // { userId: 'abc123' }
  console.log("Body:", req.body);
  return res.end(`user id was: ${req.params.userId}\n`);
});

server.listen(5150);
> curl -X POST -d '{"message" : "Hello World!"}' "http://localhost:5150/user/abc123/email?authToken=1234"

Features

  • Chainable middleware
  • familiar req.params and req.query are there
  • identical routing to what you are used to

Any Fileserver

  • Ecstatic
  • express.static
  • fs

How to handle POST (this autodetects json or form querystring)

var http = require("http");
var router = require("router-middleware");
var app = router();
var server = http.createServer(app);

// router.bodyParser auto-detects json or querystring and places the result
// on the req.body

app.post("/user/email", router.bodyParser, function (req, res, next) {
  // Now req.body will be populated with the body posted.
  // req.body.username == 'Manny';
  // req.body.species == 'cat';
});

server.listen(5150);

How to do a simple GET Fall-Through

var http = require("http");
var router = require("router-middleware");
var ecstatic = require("ecstatic")({ root: __dirname });
var app = router();
app.fileserver(ecstatic);
var server = http.createServer(app);

app.get("/admin", function (req, res, next) {
  if (some_condition) {
    next(); // will now pass through to the fileserver
    // i.e. /admin/index.html or /admin.html
  } else {
    res.writeHead(403);
    res.write("Denied, sorry");
    res.end();
  }
});
server.listen(5150);

Example

var http = require("http");
var router = require("router-middleware");
var app = router();
var server = http.createServer(app);

app.get("/user/:username", function (req, res, next) {
  res.writeHead(200);
  res.end("Hello " + req.params.username + "!");
});

server.listen(5150);

// GET /user/joe
// Hello joe!

With Fileserver Ecstatic

var http = require("http");
var router = require("router-middleware");
var app = router();
var ecstatic = require("ecstatic")({ root: __dirname });
var server = http.createServer(app);

app.fileserver(ecstatic);

// any custom routes you set will have precedence
// all other GET requests falls-through to the fileserver

With Fileserver Express

var http = require("http");
var router = require("router-middleware");
var app = router();
var express = require("express");
var server = http.createServer(app);

app.fileserver(express.static("mydirectory"));

// any custom routes you set will have precedence
// all other GET requests falls-through to the fileserver

Main Methods

.[method] (get, post, ... etc)

Attach a handler to any HTTP Method from the full method verb list Handler has the signature function(req, res, next).

.get

app.get("/user/email", function (req, res, next) {
  res.write("[email protected]");
  res.end();
});

.post

This module comes with a POST body consumer that places the POST body on the req.body for you. If you want to use this simply add it in your middleware stack for a route.

Then you can specify a route like the following:

// suppose we send JSON payload via POST
// { username: 'Manny', species: 'cat' }

app.post("/user/email", router.bodyParser, function (req, res, next) {
  // req.body will be the JSON parsed object that is sent on the post
  // req.body.username == 'Manny';
  // req.body.species == 'cat';
});

.use

Add a use handler that is placed in front of every call.

app.use(logger);
app.use(parser);

.fileserver(yourFileServer)

Attach any fileserver. Any custom routes you set will have precedence. All other unmatched GET requests falls-through to the fileserver.

Example

app.fileserver(require("ecstatic")({ root: __dirname + "/web" }));

Accessory methods

.set

app.set("<key>", "<value>"); // specify the views directory

License

The MIT License (MIT) Copyright (c) 2020 David Wee - [email protected]

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.