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

feat: display server's listening address #359

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
26 changes: 21 additions & 5 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const connect = require('connect');
const http = require('http');
const { underline } = require('picocolors');
const { underline, bold } = require('picocolors');
const Promise = require('bluebird');
const open = require('open');
const net = require('net');
Expand All @@ -21,14 +21,19 @@ module.exports = function(args) {

return this.watch();
}).then(() => startServer(http.createServer(app), port, ip)).then(server => {
const listeningAddress = getListeningAddress(server.address());
this.log.info('hexo-server listening on %s . Press Ctrl+C to stop.', bold(listeningAddress));

return server;
}).then(server => {
const addr = server.address();
const addrString = formatAddress(ip || addr.address, addr.port, root);
const localAddress = getLocalAddress(ip || addr.address, addr.port, root);

this.log.info('Hexo is running at %s . Press Ctrl+C to stop.', underline(addrString));
this.log.info('Preview your site via: %s ', underline(localAddress));
this.emit('server');

if (args.o || args.open) {
open(addrString);
open(localAddress);
}

return server;
Expand Down Expand Up @@ -69,7 +74,18 @@ function checkPort(ip, port) {
}).then(() => { server.close(); });
}

function formatAddress(ip, port, root) {
function getListeningAddress(serverAddress) {
const {address, port, family} = serverAddress;
let host;
if (family === 'IPv6') {
host = `[${address}]`;
} else {
host = address;
}
return `${host}:${port}`;
}

function getLocalAddress(ip, port, root) {
let hostname = ip;
if (ip === '0.0.0.0' || ip === '::') {
hostname = 'localhost';
Expand Down
18 changes: 16 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ describe('server', () => {
stub.callsFake(spy);

return Promise.using(prepareServer(), app => {
spy.args[1][1].should.contain('localhost');
spy.args[2][1].should.contain('localhost');
}).finally(() => {
stub.restore();
});
Expand All @@ -271,7 +271,21 @@ describe('server', () => {
stub.callsFake(spy);

return Promise.using(prepareServer({ip: '::'}), app => {
spy.args[1][1].should.contain('localhost');
spy.args[2][1].should.contain('localhost');
}).finally(() => {
stub.restore();
});
});

it('display listening address', () => {
const spy = sinon.spy();
const stub = sinon.stub(hexo.log, 'info');
stub.callsFake(spy);

return Promise.using(prepareServer({ip: 'localhost'}), app => {
spy.args[1][1].should.satisfy(listenAddress => {
return listenAddress.includes('::1') || listenAddress.includes('127.0.0.1');
});
}).finally(() => {
stub.restore();
});
Expand Down