Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

express-redirect

Package Overview
Dependencies
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

express-redirect - npm Package Compare versions

Comparing version 1.0.4 to 1.1.0

test/fixtures/compile.json

21

lib/parsePath.js

@@ -9,14 +9,15 @@ // expose parser

, after = "";
if(typeof path == "string") {
// RegExp based on TJ Holowaychuk's pathRegexp
// https://github.com/visionmedia/express/blob/master/lib/utils.js#L268
path.replace(/(.*?)((?:\/)?:(\w+)(?:\((.*?)\))?(\?)?)/g, function(_, before, whole, key, def, optional, offset, path) {
keys.push({ name: key, optional: !!optional, def: def });
elements.push(before);
after = path.substr(path.indexOf(whole) + whole.length);
});
if(after) elements.push(after);
if(elements.length == 0) elements.push(path);
}
// RegExp based on TJ Holowaychuk's pathRegexp
// https://github.com/visionmedia/express/blob/master/lib/utils.js#L268
path.replace(/(.*?)((?:\/)?:(\w+)(?:\((.*?)\))?(\?)?)/g, function(_, before, whole, key, def, optional, offset, path) {
keys.push({ name: key, optional: !!optional, def: def });
elements.push(before);
after = path.substr(path.indexOf(whole) + whole.length);
});
if(after) elements.push(after);
if(elements.length == 0) elements.push(path);
return { keys: keys, elements: elements };
}
// module dependencies
var parsePath = require("./parsePath")
, precompile = require("./precompile")
, compile = require("./compile");
, compile = require("./compile")
, sanitize = require("sanitize-arguments");

@@ -15,21 +16,31 @@ // expose redirect plugin

// app.redirect(route, target, status)
function redirect(route, target, status, method) {
if(typeof status == "string") {
var method = status
, status = 302;
function redirect(route, target, status, method, check) {
var args = sanitize(arguments, redirect, [[String, RegExp], ["/"], [302], ["all"], [Function, false]])
, status = args.status
, method = args.method.toLowerCase()
, target = precompile(parsePath(args.route), parsePath(args.target))
, check = args.check;
// register route
if(!check) {
this[method](route, function(req, res, next) {
var compiled = compile(req.params, target);
if(compiled != req.url) res.redirect(status, compiled);
else next();
});
} else {
this[method](route, function(req, res, next) {
var compiled = compile(req.params, target)
, result = check(route, compiled, req, res);
if(result === false) return next();
else if(typeof result == "string") res.redirect(status, result);
else if(compiled != req.url) res.redirect(status, compiled);
else next();
});
}
var status = status || 302
, method = (method || "all").toLowerCase()
, target = precompile(parsePath(route), parsePath(target));
// register route
this[method](route, function(req, res, next) {
var compiled = compile(req.params, target);
if(compiled != req.url) res.redirect(status, compiled);
else next();
});
// chaining
return this;
}
{
"name": "express-redirect",
"description": "Flexible redirection plugin",
"version": "1.0.4",
"version": "1.1.0",
"author": "Jan Buschtöns <buschtoens@gmail.com>",

@@ -15,3 +15,4 @@ "contributors": [

"scripts": { "prepublish": "npm prune", "test": "mocha -R spec -r should" },
"dependencies": { "sanitize-arguments": "~2.0" },
"devDependencies": { "mocha": "~1.7.3", "should": "~1.2.1", "express": "~3" }
}
# express-redirect [![Build Status](https://secure.travis-ci.org/silvinci/express-redirect.png?branch=master)](https://travis-ci.org/silvinci/express-redirect)
**express-redirect** offers you simple and blazing fast redirection rules.
Even very complex redirect rules don't take longer than 1 ms.
It just comes down to concatenating strings.
Say goodbye to writing 10 lines of code for a plain redirection over and over again.
## Installation
```
$ npm install express-redirect
```
```javascript
var express = require("express")
, redirect = require("express-redirect");
var app = express();
redirect(app); // mount the plugin
```
## Example
```javascript
// just a smple redirect

@@ -29,7 +49,39 @@ app.redirect("/p/:id", "/page/:id");

**express-redirect** is *insanely* fast, as it precompiles and joins together everything it can.
Even very complex redirect rules don't take longer than 1 ms. It just comes down to concatenating strings.
## API
express-redirect mounts the new method `app.redirect(route, target, [status], [method])` to your app.
You can access it just like `app.get()` or `app.post()` etc.
---
### route
The parameter `route` is a string and is required.
It can contain parameters like `:id`, `:year([0-9]{4})?` or `:action(view|edit)`.
It's basically just the same as a route you would pass to `app.get()`.
### target
The parameter `target` is a string and is required.
It can contain parameters like `:id`, `:year?` or `:action(view)`,
where a `?` marks an optional parameter and `(someString)` is a default value.
The parameters get replaced by their respective counterparts in the `route`.
```javascript
app.redirect("/a/:id([0-9]+)?", "/b/:id(1)");
app.redirect("/c/:action(view|delete)?", "/d/:action?");
```
```
/a -> /b/1
/a/100 -> /b/100
/c -> /d
/c/view -> /d/view
```
### status
The parameter `status` is an integer and is optional.
It is a [HTTP (redirection) status code](http://en.wikipedia.org/wiki/List_of_HTTP_status_codes#3xx_Redirection).
It defaults to [`307` (Temporary Redirect)](http://en.wikipedia.org/wiki/List_of_HTTP_status_codes#307).
### method
The parameter `method` is a string and is optional.
It is a [VERB](http://expressjs.com/api.html#app.VERB) as in express' router.
It defaults to `all`.
## License

@@ -36,0 +88,0 @@

@@ -15,3 +15,4 @@ // get the lib

for(var key in testcase.keys)
if("def" in testcase.keys[key]) testcase.keys[key].def = testcase.keys[key].def || undefined;
if("def" in testcase.keys[key])
testcase.keys[key].def = testcase.keys[key].def || undefined;

@@ -18,0 +19,0 @@ parsed

@@ -17,3 +17,4 @@ // get the lib

for(var key in testcase.keys)
if("def" in testcase.keys[key]) testcase.keys[key].def = testcase.keys[key].def || undefined;
if("def" in testcase.keys[key])
testcase.keys[key].def = testcase.keys[key].def || undefined;

@@ -20,0 +21,0 @@ precompiled

SocketSocket SOC 2 Logo

Product

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc