Skip to content

Commit 352ee1c

Browse files
committed
[fix dist] Update to "common-style".
1 parent ea89551 commit 352ee1c

File tree

4 files changed

+45
-46
lines changed

4 files changed

+45
-46
lines changed

bin/http-server

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -13,54 +13,56 @@ var ifaces = os.networkInterfaces();
1313

1414
if (argv.h || argv.help) {
1515
console.log([
16-
"usage: http-server [path] [options]",
17-
"",
18-
"options:",
19-
" -p Port to use [8080]",
20-
" -a Address to use [0.0.0.0]",
21-
" -d Show directory listings [true]",
22-
" -i Display autoIndex [true]",
23-
" -e --ext Default file extension if none supplied [none]",
24-
" -s --silent Suppress log messages from output",
25-
" --cors Enable CORS via the 'Access-Control-Allow-Origin' header",
26-
" -o [path] Open browser window after starting the server",
27-
" -c Cache time (max-age) in seconds [3600], e.g. -c10 for 10 seconds.",
28-
" To disable caching, use -c-1.",
29-
"",
30-
" -P --proxy Fallback proxy if the request cannot be resolved. e.g.: http://someurl.com",
31-
"",
32-
" -S --ssl Enable https.",
33-
" -C --cert Path to ssl cert file (default: cert.pem).",
34-
" -K --key Path to ssl key file (default: key.pem).",
35-
"",
36-
" -r --robots Respond to /robots.txt [User-agent: *\\nDisallow: /]",
37-
" -h --help Print this list and exit."
16+
'usage: http-server [path] [options]',
17+
'',
18+
'options:',
19+
' -p Port to use [8080]',
20+
' -a Address to use [0.0.0.0]',
21+
' -d Show directory listings [true]',
22+
' -i Display autoIndex [true]',
23+
' -e --ext Default file extension if none supplied [none]',
24+
' -s --silent Suppress log messages from output',
25+
' --cors Enable CORS via the "Access-Control-Allow-Origin" header',
26+
' -o [path] Open browser window after starting the server',
27+
' -c Cache time (max-age) in seconds [3600], e.g. -c10 for 10 seconds.',
28+
' To disable caching, use -c-1.',
29+
'',
30+
' -P --proxy Fallback proxy if the request cannot be resolved. e.g.: http://someurl.com',
31+
'',
32+
' -S --ssl Enable https.',
33+
' -C --cert Path to ssl cert file (default: cert.pem).',
34+
' -K --key Path to ssl key file (default: key.pem).',
35+
'',
36+
' -r --robots Respond to /robots.txt [User-agent: *\\nDisallow: /]',
37+
' -h --help Print this list and exit.'
3838
].join('\n'));
3939
process.exit();
4040
}
4141

4242
var port = argv.p || parseInt(process.env.PORT, 10),
4343
host = argv.a || '0.0.0.0',
44-
log = (argv.s || argv.silent) ? (function () {}) : console.log,
4544
ssl = !!argv.S || !!argv.ssl,
4645
proxy = argv.P || argv.proxy,
47-
requestLogger;
46+
logger = colors && {};
4847

4948
if (!argv.s && !argv.silent) {
50-
requestLogger = function(req, res, error) {
51-
var date = (new Date).toUTCString();
52-
if (error) {
53-
log('[%s] "%s %s" Error (%s): "%s"', date, req.method.red, req.url.red, error.status.toString().red, error.message.red);
54-
} else {
55-
log('[%s] "%s %s" "%s"', date, req.method.cyan, req.url.cyan, req.headers['user-agent']);
49+
logger = {
50+
info: console.log,
51+
request: function (req, res, error) {
52+
var date = new Date().toUTCString();
53+
if (error) {
54+
logger.info('[%s] "%s %s" Error (%s): "%s"', date, req.method.red, req.url.red, error.status.toString().red, error.message.red);
55+
} else {
56+
logger.info('[%s] "%s %s" "%s"', date, req.method.cyan, req.url.cyan, req.headers['user-agent']);
57+
}
5658
}
5759
};
5860
}
5961

6062
if (!port) {
6163
portfinder.basePort = 8080;
6264
portfinder.getPort(function (err, port) {
63-
if (err) throw err;
65+
if (err) { throw err; }
6466
listen(port);
6567
});
6668
} else {
@@ -75,7 +77,7 @@ function listen(port) {
7577
autoIndex: argv.i,
7678
robots: argv.r || argv.robots,
7779
ext: argv.e || argv.ext,
78-
logFn: requestLogger,
80+
logFn: logger.request,
7981
proxy: proxy
8082
};
8183

@@ -95,9 +97,9 @@ function listen(port) {
9597
var canonicalHost = host === '0.0.0.0' ? '127.0.0.1' : host,
9698
protocol = ssl ? 'https:' : 'http:';
9799

98-
log('Starting up http-server, serving '.yellow
100+
logger.info('Starting up http-server, serving '.yellow
99101
+ server.root.cyan
100-
+ (ssl ? (' through'.yellow + ' https'.cyan) : '')
102+
+ ssl ? ' through'.yellow + ' https'.cyan : ''
101103
+ ' on: '.yellow
102104
+ (protocol + '//' + host + ':' + port).cyan);
103105

@@ -112,10 +114,10 @@ function listen(port) {
112114
});
113115

114116
if (typeof proxy === 'string') {
115-
log('Unhandled requests will be served from: ' + proxy);
117+
logger.info('Unhandled requests will be served from: ' + proxy);
116118
}
117119

118-
log('Hit CTRL-C to stop the server');
120+
logger.info('Hit CTRL-C to stop the server');
119121
if (argv.o) {
120122
opener(
121123
protocol + '//' + canonicalHost + ':' + port,
@@ -134,8 +136,8 @@ if (process.platform === 'win32') {
134136
});
135137
}
136138

137-
process.on('SIGINT', function() {
138-
log('http-server stopped.'.red);
139+
process.on('SIGINT', function () {
140+
logger.info('http-server stopped.'.red);
139141
process.exit();
140142
});
141143

lib/http-server.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
var fs = require('fs'),
2-
util = require('util'),
32
union = require('union'),
43
ecstatic = require('ecstatic'),
54
httpProxy = require('http-proxy'),
@@ -10,13 +9,11 @@ var HTTPServer = exports.HTTPServer = function (options) {
109

1110
if (options.root) {
1211
this.root = options.root;
13-
}
14-
else {
12+
} else {
1513
try {
1614
fs.lstatSync('./public');
1715
this.root = './public';
18-
}
19-
catch (err) {
16+
} catch (err) {
2017
this.root = './';
2118
}
2219
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
],
1414
"scripts": {
1515
"start": "node ./bin/http-server",
16-
"pretest": "stylezero bin/ lib/ test",
16+
"pretest": "stylezero bin/http-server lib/ test",
1717
"test": "vows --spec --isolate"
1818
},
1919
"contributors": [

test/http-server-test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ vows.describe('http-server').addBatch({
7171
topic: function () {
7272
request('http://127.0.0.1:8080/', this.callback);
7373
},
74-
'should respond with headers set in options': function (err, res, body) {
74+
'should respond with headers set in options': function (err, res) {
7575
assert.equal(res.headers['access-control-allow-origin'], '*');
7676
assert.equal(res.headers['access-control-allow-credentials'], 'true');
7777
}
@@ -146,7 +146,7 @@ vows.describe('http-server').addBatch({
146146
}
147147
}, this.callback);
148148
},
149-
'status code should be 204': function (err, res, body) {
149+
'status code should be 204': function (err, res) {
150150
assert.equal(res.statusCode, 204);
151151
}
152152
}

0 commit comments

Comments
 (0)