Permalink
Browse files

Add redirect middleware module

  • Loading branch information...
1 parent 4146800 commit f4a65a8f4c3c015053aa0c08192e860e7399bd57 @kriszyp kriszyp committed Nov 7, 2011
Showing with 29 additions and 0 deletions.
  1. +29 −0 jsgi/redirect.js
View
@@ -0,0 +1,29 @@
+var uri = require("../util/url");
+
+exports.Redirect = function (path, status) {
+
+ status = status || 301;
+
+ return function (request) {
+ var location =
+ (request.scheme || "http") +
+ "://" +
+ (request.headers.host || (
+ request.host +
+ (request.port == 80 ? "" : ":" + request.port)
+ )) +
+ (request.scriptName || "") +
+ request.pathInfo;
+
+ location = path ? uri.resolve(location, path) : request.headers.referer;
+
+ return {
+ status: status,
+ headers: {
+ "location": location,
+ "content-type": "text/plain"
+ },
+ body: ['Go to <a href="' + location + '">' + location + "</a>"]
+ }
+ }
+}

0 comments on commit f4a65a8

Please sign in to comment.