File tree Expand file tree Collapse file tree 4 files changed +32
-4
lines changed Expand file tree Collapse file tree 4 files changed +32
-4
lines changed Original file line number Diff line number Diff line change @@ -62,6 +62,8 @@ This will install `http-server` globally so that it may be run from the command
62
62
63
63
` -o ` Open browser window after staring the server
64
64
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
+
65
67
` -P ` or ` --proxy ` Proxies all requests which can't be resolved locally to the given url. e.g.: -P http://someurl.com
66
68
67
69
` -S ` or ` --ssl ` Enable https.
@@ -70,7 +72,6 @@ This will install `http-server` globally so that it may be run from the command
70
72
71
73
` -K ` or ` --key ` Path to ssl key file (default: key.pem).
72
74
73
- ` -h ` or ` --help ` Print this list and exit.
75
+ ` -r ` or ` --robots ` Provide a /robots.txt (whose content defaults to 'User-agent: * \nDisallow: /')
74
76
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.
Original file line number Diff line number Diff line change 1
1
#!/usr/bin/env node
2
2
3
- var colors = require ( 'colors' ) ,
3
+ var colors = require ( 'colors' ) ,
4
4
httpServer = require ( '../lib/http-server' ) ,
5
5
portfinder = require ( 'portfinder' ) ,
6
6
opener = require ( 'opener' ) ,
@@ -30,6 +30,7 @@ if (argv.h || argv.help) {
30
30
" -C --cert Path to ssl cert file (default: cert.pem)." ,
31
31
" -K --key Path to ssl key file (default: key.pem)." ,
32
32
"" ,
33
+ " -r --robots Respond to /robots.txt [User-agent: *\\nDisallow: /]" ,
33
34
" -h --help Print this list and exit."
34
35
] . join ( '\n' ) ) ;
35
36
process . exit ( ) ;
@@ -69,6 +70,7 @@ function listen(port) {
69
70
cache : argv . c ,
70
71
showDir : argv . d ,
71
72
autoIndex : argv . i ,
73
+ robots : argv . r || argv . robots ,
72
74
ext : argv . e || argv . ext ,
73
75
logFn : requestLogger ,
74
76
proxy : proxy
Original file line number Diff line number Diff line change @@ -50,6 +50,21 @@ var HTTPServer = exports.HTTPServer = function (options) {
50
50
before . push ( corser . create ( ) ) ;
51
51
}
52
52
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
+
53
68
before . push ( ecstatic ( {
54
69
root : this . root ,
55
70
cache : this . cache ,
Original file line number Diff line number Diff line change @@ -12,11 +12,13 @@ vows.describe('http-server').addBatch({
12
12
topic : function ( ) {
13
13
var server = httpServer . createServer ( {
14
14
root : root ,
15
+ robots : true ,
15
16
headers : {
16
17
'Access-Control-Allow-Origin' : '*' ,
17
18
'Access-Control-Allow-Credentials' : 'true'
18
19
}
19
20
} ) ;
21
+
20
22
server . listen ( 8080 ) ;
21
23
this . callback ( null , server ) ;
22
24
} ,
@@ -57,6 +59,14 @@ vows.describe('http-server').addBatch({
57
59
assert . include ( body , '/canYouSeeMe' ) ;
58
60
}
59
61
} ,
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
+ } ,
60
70
'and options include custom set http-headers' : {
61
71
topic : function ( ) {
62
72
request ( 'http://127.0.0.1:8080/' , this . callback ) ;
You can’t perform that action at this time.
0 commit comments