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

Accept HOST as option to server creation #6

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
Next Next commit
Accept host as a setting
mariovalney committed Feb 2, 2022
commit ce270518fc785064adedb8463950822c6bad7356
13 changes: 12 additions & 1 deletion src/bin.js
Original file line number Diff line number Diff line change
@@ -2,5 +2,16 @@
import { start } from './index.js';

const root = process.argv[2];
const args = { live: false, root };

start({ live: false, root });
const port = process.argv[3];
if (port) {
args.port = port;
}

const host = process.argv[4];
if (host) {
args.host = host;
}

start(args);
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -16,6 +16,7 @@ import {
} from './utils/index.js';

export let options = {
host: 'localhost',
port: 7000,
root: '.',
live: true
8 changes: 5 additions & 3 deletions src/listen.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import logMessage from './logMessage.js';
import { options } from './index.js';

const listen = (server, serverPort = options.port) => {

const listen = (server, serverPort = options.port, serverHost = options.host) => {
server
.listen(serverPort, () => {
.listen(serverPort, serverHost, () => {
const currentPort = server.address().port;
const currentHost = server.address().address;

logMessage(currentPort);
logMessage(currentPort, currentHost);
})
.once('error', () => {
server.removeAllListeners('listening');
4 changes: 2 additions & 2 deletions src/logMessage.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { log, error, getIp } from './utils/index.js';
import { options } from './index.js';

const logMessage = currentPort => {
const logMessage = (currentPort, currentHost) => {
const { port } = options;

log('\nServing 🍛\n');
log(`Local → http://localhost:${currentPort}\n`);
log(`Local → http://${currentHost}:${currentPort}\n`);
log(`Network → http://${getIp()}:${currentPort}\n`);
if (currentPort != port) error(`Port ${port} was in use.\n`);
};