Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added possibility to use reroutes #365

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ This will install `http-server` globally so that it may be run from the command

`-r` or `--robots` Provide a /robots.txt (whose content defaults to 'User-agent: *\nDisallow: /')

`-R` or `--reroute` Reroute file. json array with objects containing `match`, `replace`, `abortOnMatch` [`true`], match should contain two keys: `regexp` (regexp string) and `flags` (string with flags)

`-h` or `--help` Print this list and exit.

# Development
Expand Down
33 changes: 32 additions & 1 deletion bin/http-server
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var colors = require('colors/safe'),
httpServer = require('../lib/http-server'),
portfinder = require('portfinder'),
opener = require('opener'),
fs = require('fs'),
argv = require('optimist')
.boolean('cors')
.argv;
Expand Down Expand Up @@ -39,6 +40,8 @@ if (argv.h || argv.help) {
' -K --key Path to ssl key file (default: key.pem).',
'',
' -r --robots Respond to /robots.txt [User-agent: *\\nDisallow: /]',
' -R --reroute Reroute file. json array with objects containing match, replace, abortOnMatch[true]',
' match should contain two keys: regexp (regexp string) and flags (string with flags)',
' -h --help Print this list and exit.'
].join('\n'));
process.exit();
Expand All @@ -49,6 +52,7 @@ var port = argv.p || parseInt(process.env.PORT, 10),
ssl = !!argv.S || !!argv.ssl,
proxy = argv.P || argv.proxy,
utc = argv.U || argv.utc,
reroutes,
logger;

if (!argv.s && !argv.silent) {
Expand All @@ -70,6 +74,10 @@ if (!argv.s && !argv.silent) {
req.headers['user-agent']
);
}
},
reroute: function (prevUrl, newUrl) {
var date = utc ? new Date().toUTCString() : new Date();
logger.info('[%s] Rerouted "%s" to "%s"', date, colors.cyan(prevUrl), colors.cyan(newUrl));
}
};
}
Expand All @@ -91,6 +99,27 @@ else {
listen(port);
}

if (argv.R || argv.reroute) {
reroutes = JSON.parse(fs.readFileSync(argv.R || argv.reroute));
if (!Array.isArray(reroutes)) {
throw new Error('Reroute file must be an array.');
}
reroutes.forEach(function (elem) {
if (elem.match && elem.replace) {
elem.match = new RegExp(elem.match.regexp, elem.match.flags || '');
if (elem.replace.charAt(0) !== '/' && !argv.s && !argv.silent) {
logger.info(colors.red("URL:s not starting with slash won't be found: " + elem.replace));
}
}
else {
throw new Error('Each reroute must have the match key and replace key');
}
if (typeof elem.abortOnMatch === 'undefined') {
elem.abortOnMatch = true;
}
});
}

function listen(port) {
var options = {
root: argv._[0],
Expand All @@ -101,7 +130,9 @@ function listen(port) {
robots: argv.r || argv.robots,
ext: argv.e || argv.ext,
logFn: logger.request,
proxy: proxy
logReroute: logger.reroute,
proxy: proxy,
reroutes: reroutes
};

if (argv.cors) {
Expand Down
19 changes: 19 additions & 0 deletions lib/http-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,25 @@ function HttpServer(options) {

var before = options.before ? options.before.slice() : [];

if (options.reroutes) {
before.push(function (req, res) {
for (var i = 0; i < options.reroutes.length; i++) {
var reroute = options.reroutes[i];
var prevUrl = req.url;
req.url = req.url.replace(reroute.match, reroute.replace);
if (prevUrl !== req.url) {
if (options.logReroute) {
options.logReroute(prevUrl, req.url);
}
if (reroute.abortOnMatch) {
break;
}
}
}
res.emit('next');
});
}

before.push(function (req, res) {
if (options.logFn) {
options.logFn(req, res);
Expand Down
Binary file added public/img/turtle_reroute.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
<h1>Serving up static files like they were turtles strapped to rockets.</h1>

<img src="./img/turtle.png"/>

<h2>Tip: try adding -R public/reroutes.json to the command and see what happens to the turtle image!</h2>
<p>You might have to hit ctrl+f5 to refresh the page</p>
</body>
</html>

6 changes: 6 additions & 0 deletions public/reroutes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
{
"match": {"regexp":"/img/([a-z]+).png", "flags":"i"},
"replace": "/img/$1_reroute.png"
}
]