Commits

Magnus Hoff committed 38a998b

RedirectResource. Can redirect in several ways

Comments (0)

Files changed (2)

permanentredirectresource.js

-var url = require('url');
 var verifyNew = require('./util').verifyNew;
-var Resource = require('./resource').Resource;
+var RedirectResource = require('./redirectresource').RedirectResource;
 
 
 function PermanentRedirectResource(relativeTargetUrl) {
     verifyNew(this, this.constructor.name);
-    Resource.call(this);
+    RedirectResource.call(this, RedirectResource.MOVED_PERMANENTLY, relativeTargetUrl);
 
-    function redirect(req, res) {
-        var parsedUrl = url.parse(req.url);
-        var targetPath = url.resolve(parsedUrl.path, relativeTargetUrl);
-        parsedUrl.pathname = targetPath;
-        var targetUrl = url.format(parsedUrl);
-
-        res.writeHead(301, {
-            "Location": targetUrl,
-            "Content-Type": "text/plain"
-        });
-
-        if (req.method !== 'HEAD') {
-            res.write("Go see " + targetUrl);
-        }
-        res.end();
-    }
-
-    this.http_GET = redirect;
-    this.http_HEAD = redirect;
-    this.http_POST = redirect;
-    this.http_PUT = redirect;
-    this.http_DELETE = redirect;
+    console.log("DEPRECATED: PermanentRedirectResource is deprecated in favor of " +
+        "'new RedirectResource(RedirectResource.MOVED_PERMANENTLY, targetUrl)'");
 }
-
-PermanentRedirectResource.prototype = new Resource();
+PermanentRedirectResource.prototype = new RedirectResource();
 PermanentRedirectResource.constructor = PermanentRedirectResource;
 
 

redirectresource.js

+var url = require('url');
+var verifyNew = require('./util').verifyNew;
+var Resource = require('./resource').Resource;
+
+
+function RedirectResource(redirectType, relativeTargetUrl) {
+    verifyNew(this, this.constructor.name);
+    Resource.call(this);
+
+    function redirect(req, res) {
+        var parsedUrl = url.parse(req.url);
+        var targetPath = url.resolve(parsedUrl.path, relativeTargetUrl);
+        parsedUrl.pathname = targetPath;
+        var targetUrl = url.format(parsedUrl);
+
+        res.writeHead(redirectType, {
+            "Location": targetUrl,
+            "Content-Type": "text/html"
+        });
+
+        if (req.method !== 'HEAD') {
+            res.write('<!DOCTYPE html>\n');
+            res.write('<a href="' + targetUrl + '">Go see ' + targetUrl + '</a>');
+        }
+        res.end();
+    }
+
+    this.http_GET = redirect;
+    this.http_HEAD = redirect;
+    this.http_POST = redirect;
+    this.http_PUT = redirect;
+    this.http_DELETE = redirect;
+}
+
+RedirectResource.prototype = new Resource();
+RedirectResource.constructor = RedirectResource;
+
+
+exports.RedirectResource = RedirectResource;
+exports.MOVED_PERMANENTLY = 301; // Always go here instead of where you were
+exports.FOUND = 302; // Obsolete. Prefer TEMPORARY_REDIRECT or SEE_OTHER
+exports.SEE_OTHER = 303; // Go here *and* switch to GET
+exports.TEMPORARY_REDIRECT = 307; // Go here and make the *same* request
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.