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

Serve on all interfaces by default #39

Merged
merged 1 commit into from
Nov 9, 2017
Merged
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: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ $ hexo server

Option | Description | Default
--- | --- | ---
`-i`, `--ip` | Override the default server IP. | 0.0.0.0
`-i`, `--ip` | Override the default server IP. | `::` when IPv6 is available, else `0.0.0.0` (note: in most systems, `::` also binds to `0.0.0.0`)
`-p`, `--port` | Override the default port. | 4000
`-s`, `--static` | Only serve static files. | false
`-l`, `--log [format]` | Enable logger. Override log format. | false
Expand Down
16 changes: 10 additions & 6 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ var connect = require('connect');
var http = require('http');
var chalk = require('chalk');
var Promise = require('bluebird');
var format = require('util').format;
var open = require('opn');
var net = require('net');
var url = require('url');

module.exports = function(args) {
var app = connect();
var config = this.config;
var ip = args.i || args.ip || config.server.ip || 'localhost';
var ip = args.i || args.ip || config.server.ip || undefined;
var port = parseInt(args.p || args.port || config.server.port || process.env.port, 10) || 4000;
var root = config.root;
var self = this;
Expand All @@ -27,9 +27,10 @@ module.exports = function(args) {
}).then(function() {
return startServer(http.createServer(app), port, ip);
}).then(function(server) {
var addr = formatAddress(ip, port, root);
var addr = server.address();
var addrString = formatAddress(ip || addr.address, addr.port, root);

self.log.info('Hexo is running at %s. Press Ctrl+C to stop.', chalk.underline(addr));
self.log.info('Hexo is running at %s. Press Ctrl+C to stop.', chalk.underline(addrString));
self.emit('server');

if (args.o || args.open) {
Expand Down Expand Up @@ -83,7 +84,10 @@ function checkPort(ip, port) {
}

function formatAddress(ip, port, root) {
if (ip === '0.0.0.0') ip = 'localhost';
var hostname = ip;
if (ip === '0.0.0.0' || ip === '::') {
hostname = 'localhost';
}

return format('http://%s:%d%s', ip, port, root);
return url.format({protocol: 'http', hostname: hostname, port: port, path: root});
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"author": "Tommy Chen <[email protected]> (http://zespia.tw)",
"maintainers": [
"Abner Chou <[email protected]> (http://abnerchou.me)"
],
],
"license": "MIT",
"dependencies": {
"bluebird": "^3.0.6",
Expand Down
11 changes: 11 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,4 +241,15 @@ describe('server', function() {
hexo.log.info.restore();
});
});

it('display localhost instead of [::]', function() {
var spy = sinon.spy();
sinon.stub(hexo.log, 'info', spy);

return Promise.using(prepareServer({ip: '::'}), function(app) {
spy.args[1][1].should.contain('localhost');
}).finally(function() {
hexo.log.info.restore();
});
});
});