Skip to content

Commit ee6a65b

Browse files
andrefsindexzero
authored andcommitted
Add option to dynamically serve a /robots.txt
1 parent ca4aee6 commit ee6a65b

File tree

4 files changed

+32
-4
lines changed

4 files changed

+32
-4
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ This will install `http-server` globally so that it may be run from the command
6262

6363
`-o` Open browser window after staring the server
6464

65+
`-c` Set cache time (in seconds) for cache-control max-age header, e.g. -c10 for 10 seconds (defaults to '3600'). To disable caching, use -c-1.
66+
6567
`-P` or `--proxy` Proxies all requests which can't be resolved locally to the given url. e.g.: -P http://someurl.com
6668

6769
`-S` or `--ssl` Enable https.
@@ -70,7 +72,6 @@ This will install `http-server` globally so that it may be run from the command
7072

7173
`-K` or `--key` Path to ssl key file (default: key.pem).
7274

73-
`-h` or `--help` Print this list and exit.
75+
`-r` or `--robots` Provide a /robots.txt (whose content defaults to 'User-agent: *\nDisallow: /')
7476

75-
`-c` Set cache time (in seconds) for cache-control max-age header, e.g. -c10 for 10 seconds
76-
(defaults to '3600'). To disable caching, use -c-1.
77+
`-h` or `--help` Print this list and exit.

bin/http-server

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env node
22

3-
var colors = require('colors'),
3+
var colors = require('colors'),
44
httpServer = require('../lib/http-server'),
55
portfinder = require('portfinder'),
66
opener = require('opener'),
@@ -30,6 +30,7 @@ if (argv.h || argv.help) {
3030
" -C --cert Path to ssl cert file (default: cert.pem).",
3131
" -K --key Path to ssl key file (default: key.pem).",
3232
"",
33+
" -r --robots Respond to /robots.txt [User-agent: *\\nDisallow: /]",
3334
" -h --help Print this list and exit."
3435
].join('\n'));
3536
process.exit();
@@ -69,6 +70,7 @@ function listen(port) {
6970
cache: argv.c,
7071
showDir: argv.d,
7172
autoIndex: argv.i,
73+
robots: argv.r || argv.robots,
7274
ext: argv.e || argv.ext,
7375
logFn: requestLogger,
7476
proxy: proxy

lib/http-server.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,21 @@ var HTTPServer = exports.HTTPServer = function (options) {
5050
before.push(corser.create());
5151
}
5252

53+
if (options.robots) {
54+
before.push(function (req, res) {
55+
if (req.url === '/robots.txt') {
56+
res.setHeader('Content-Type', 'text/plain');
57+
var robots = options.robots === true
58+
? 'User-agent: *\nDisallow: /'
59+
: options.robots.replace(/\\n/, '\n');
60+
61+
return res.end(robots);
62+
}
63+
64+
res.emit('next');
65+
});
66+
}
67+
5368
before.push(ecstatic({
5469
root: this.root,
5570
cache: this.cache,

test/http-server-test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@ vows.describe('http-server').addBatch({
1212
topic: function () {
1313
var server = httpServer.createServer({
1414
root: root,
15+
robots: true,
1516
headers: {
1617
'Access-Control-Allow-Origin': '*',
1718
'Access-Control-Allow-Credentials': 'true'
1819
}
1920
});
21+
2022
server.listen(8080);
2123
this.callback(null, server);
2224
},
@@ -57,6 +59,14 @@ vows.describe('http-server').addBatch({
5759
assert.include(body, '/canYouSeeMe');
5860
}
5961
},
62+
'when robots options is activated': {
63+
topic: function () {
64+
request('http://127.0.0.1:8080/', this.callback);
65+
},
66+
'should respond with status code 200 to /robots.txt': function (res) {
67+
assert.equal(res.statusCode, 200);
68+
}
69+
},
6070
'and options include custom set http-headers': {
6171
topic: function () {
6272
request('http://127.0.0.1:8080/', this.callback);

0 commit comments

Comments
 (0)