Socket
Socket
Sign inDemoInstall

browserver-router

Package Overview
Dependencies
0
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    browserver-router

A platform-agnostic router for HTTP listeners that follow the node.js spec


Version published
Maintainers
1
Install size
7.97 kB
Created

Readme

Source

browserver-router

Build Status

This is a simple and unambitious router implementation that can be used in either the browser or any CommonJS environment. It was designed for browserver, but will work with any server that conforms to the node.js http.Server API.

Features

  • Small: 548 minizipped bytes of dependency-free code
  • Portable: works in the browser and on node.js
  • Easy: response is automatically generated if omitted

Example

In node.js

var Router = require("router")

var router = Router({
  "/salutation/:name": {
    GET: function(req, res) {
      res.writeHead(200)
      res.end("Hello, " + req.params[0] + ".")
    },

    DELETE: function(req, res) {
      res.writeHead(200)
      res.end("Goodbye, " + req.params[0] + ".")
    },

    "*": function(req) {
      throw new Error("No such salutation")
    }
  },

  "/method": function(req, res) {
    res.writeHead(200)
    res.end("Matched method: " + req.method)
  }
})

var http = require("http")
var server = http.createServer(router)

server.listen(8000)

In the browser with browserver and engine.io

var router = Router({
  "/salutation/:name": {
    GET: function(req, res) {
      res.writeHead(200)
      res.end("Hello, " + req.params[0] + ".")
    },

    DELETE: function(req, res) {
      res.writeHead(200)
      res.end("Goodbye, " + req.params[0] + ".")
    },

    "*": function(req) {
      throw new Error("No such salutation")
    }
  },

  "/method": function(req, res) {
    res.writeHead(200)
    res.end("Matched method: " + req.method)
  }
})

var server = http.createServer(router)
var ws = new eio.Socket({host: "myserver.com"})

server.listen(ws)

API

router = new Router([Object routes])

Creates a new router, which is a function with the standard (req, res) signature. If a routes object is passed, it will .route(key, value) will be called for each key.

var router = Router({
  "/": {
    GET: function(req, res) {
      res.writeHead(200)
      res.end("OK")
    }
  }
})

router.route([String route], [Object methodMap])

Adds a route to match. Both arguments are required.

router.route("/salutation/:name", {
  GET: function(req, res) {
    res.writeHead(200)
    res.end("Hello, " + req.params[0] + ".")
  },

  DELETE: function(req, res) {
    res.writeHead(200)
    res.end("Goodbye, " + req.params[0] + ".")
  }
})

The route string is compiled into a regular expression, using the same logic as the Backbone.js router, in which :param strings match a single url component between slashes and * splats match any number of url components. Any matching parameters are used to populate the req.params array by match position.

The methodMap object maps method names (such as GET and POST) to handlers. A fallback handler that matches all routes can be specified using * as the method name.

Handlers can either use the standard function(req, res){} signature, or a function(req){} signature with the response omitted, in which the response is automatically generated like this:

router.route("/random-error", function(req) {
  var ok = Math.random() > .5

  if (!ok) {
    // equivalent to
    // res.writeHead(500, {"Content-Type": "text/plain"})
    // res.end("An error occurred")
    throw new Error("An error occurred")
  }

  else {
    // equivalent to
    // res.writeHead(204)
    // res.end()
    return
  }
})

router.route([String route], [Function handler])

A convenient shortcut for router.route(route, {"*": handler}), to fire on any otherwise unhandled method.

Keywords

FAQs

Last updated on 01 Sep 2012

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc