diff --git a/.gitignore b/.gitignore index eabb971..d176074 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ npm-debug.log package-lock.json yarn.lock .DS_Store +yarn-error.log diff --git a/package.json b/package.json index 41292c8..219e831 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,9 @@ }, "scripts": { "format": "prettier --write --ignore-unknown '**/*'", - "start": "node src/bin.js test" + "start": "node src/bin.js test", + "start:port": "node src/bin.js test 3000", + "start:host": "node src/bin.js test 3000 0.0.0.0" }, "devDependencies": { "prettier": "^2.2.1" diff --git a/readme.md b/readme.md index e431638..808d1aa 100644 --- a/readme.md +++ b/readme.md @@ -56,6 +56,7 @@ npm init serve [folder] import serve from 'create-serve'; serve.start({ + host: 'localhost', port: 7000, root: '.', live: true diff --git a/src/bin.js b/src/bin.js index 559b87f..98111a0 100755 --- a/src/bin.js +++ b/src/bin.js @@ -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); diff --git a/src/index.js b/src/index.js index b29add5..026da69 100644 --- a/src/index.js +++ b/src/index.js @@ -16,6 +16,7 @@ import { } from './utils/index.js'; export let options = { + host: 'localhost', port: 7000, root: '.', live: true diff --git a/src/listen.js b/src/listen.js index 5232e22..29d176f 100644 --- a/src/listen.js +++ b/src/listen.js @@ -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'); diff --git a/src/logMessage.js b/src/logMessage.js index b697d51..13c70b7 100644 --- a/src/logMessage.js +++ b/src/logMessage.js @@ -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`); };