Giter VIP home page Giter VIP logo

http-route's Introduction

http-route

HTTP routing with a nestable functional style.

This module exports a function that wraps a given http handler and returns a new http handler that is only executed if the given condition is met.

Usage

var route = require("http-route");
var compose = require("http-compose");
var createServer = require("http").createServer;

createServer(compose([
  route(isWrite, checkCSRF),
  route("/hello", compose([
    route("GET /:name", sendHello),
    route("GET /", sendDefaultHello),
  ]),
]).listen(8080);

function isWrite(req) {
  return ["PUT", "POST", "DELETE"].indexOf(req.method) !== -1;
}

function checkCSRF(req, res, callback) {
  if (req.headers["x-csrf-token"] !== "magic") {
    res.statusCode = 400;
    res.end("Invalid request");
    return;
  }
  callback();
}

function sendHello(req, res) {
  res.end("hello, " + req.params.name);
}

function sendDefaultHello(req, res) {
  res.end("hello world");
}

Conditions

Conditions are functions that determine if a request matches the route or not. A condition function can contain any arbitrary logic, as long as it synchronously returns a truthy or falsey value.

There are also higher-order conditions, such as strings, that get "compiled" down to a condition function. For example, the string "GET /hello" is equivalent to the following condition function:

function(req) {
  return req.method === "GET" && req.url === "/hello";
}

Condition functions can also modify the state of the current request. For example, condition functions can modify req.url so that nested routes can match only the remaining part of the URL. They can also parse out parameters from the URL and populate something like req.params.

Functions

Any arbitrary function with a return value that will determine if the route matches or not. The function receives the current request as a parameter.

Strings

Strings can contain a method, a url, or both.

Methods

"GET" will match GET requests.

URLs

"/foo" will match requests that begin with "/foo". URLs can contain named parameters in the same way as the express router and will populate req.params. The current req.url will be modified to remove the matched part of the URL so that nested routers can match on the remaining unmatched part of the URL. The original full URL is saved in req.originalUrl.

Both

"POST /bar" will match POST requests to the exact url "/bar".

Objects

  • path: The request path must match the given path exactly
  • mount: The request path must begin with the given path and will be "mounted" under the given path (req.url will be adjusted to reflect this)
  • method: The request method must match the given method

Arrays

An array of conditions will be composed into a single condition and match only if all conditions match.

HTTP Handlers

HTTP handlers are functions that handle http requests. These are the same type of functions passed to require("http").createServer.

Composing HTTP Handlers

The http-compose module (not yet written) can be used to compose multiple http handlers into a single handler. A third callback parameter will be passed to composed functions to allow passing control to the next handler in the chain.

API

route(condition, handler)

Creates an http handler function that will only be called if the given condition matches.

Installation

npm install http-route

http-route's People

Contributors

parshap avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.