Commit f6484de4 authored by Dominic Tarr's avatar Dominic Tarr

[doc] add comments to examples/url-middleware.js

parent 45f3df80
/* /*
gzip-middleware.js: Basic example of middleware in node-http-proxy urls-middleware.js: Basic example of middleware in node-http-proxy
Copyright (c) 2010 Charlie Robbins, Mikeal Rogers, Fedor Indutny, Marak Squires, & Dominic Tarr. Copyright (c) 2010 Charlie Robbins, Mikeal Rogers, Fedor Indutny, Marak Squires, & Dominic Tarr.
...@@ -30,7 +30,9 @@ var util = require('util'), ...@@ -30,7 +30,9 @@ var util = require('util'),
httpProxy = require('./../lib/node-http-proxy'); httpProxy = require('./../lib/node-http-proxy');
// //
// Basic Http Proxy Server // url proxying middleware example.
//
// this is not optimised or tested but shows the basic approch to writing a middleware.
// //
function matcher (url, dest) { function matcher (url, dest) {
var r = new RegExp (url) var r = new RegExp (url)
...@@ -54,19 +56,19 @@ exports.urls = function (urls) { ...@@ -54,19 +56,19 @@ exports.urls = function (urls) {
return function (req, res, next) { return function (req, res, next) {
// //
// in nhp middlewares, `proxy` is the prototype of `next` // in nhp middlewares, `proxy` is the prototype of `next`
// (this means nhp middlewares support both connect API (req, res, next)
// and nhp API (req, res, proxy)
// //
var proxy = next; var proxy = next;
for (var k in matchers) { for (var k in matchers) {
var m var m;
if (m = matchers[k](req.url)) { if (m = matchers[k](req.url)) {
req.url = m.url req.url = m.url;
return proxy.proxyRequest(req,res, m.dest) return proxy.proxyRequest(req,res, m.dest);
} }
} }
} }
} }
httpProxy.createServer( httpProxy.createServer(
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment