Commit c4a22abb authored by Jesse Hattabaugh's avatar Jesse Hattabaugh Committed by Tim Neutkens

adding hostname argument to CLI (#1017)

* adding hostname argument to CLI

* using -H instead of -hn

* removing hostname default

* checking that hostname has a truthy value that's not a boolean

* making the log message match the hostname

* oops
parent 19f11255
...@@ -10,12 +10,11 @@ process.env.NODE_ENV = process.env.NODE_ENV || 'production' ...@@ -10,12 +10,11 @@ process.env.NODE_ENV = process.env.NODE_ENV || 'production'
const argv = parseArgs(process.argv.slice(2), { const argv = parseArgs(process.argv.slice(2), {
alias: { alias: {
h: 'help', h: 'help',
H: 'hostname',
p: 'port' p: 'port'
}, },
boolean: ['h'], boolean: ['h'],
default: { default: { p: 3000 }
p: 3000
}
}) })
if (argv.help) { if (argv.help) {
...@@ -33,6 +32,7 @@ if (argv.help) { ...@@ -33,6 +32,7 @@ if (argv.help) {
Options Options
--port, -p A port number on which to start the application --port, -p A port number on which to start the application
--hostname, -H Hostname on which to start the application
--help, -h Displays this message --help, -h Displays this message
`) `)
process.exit(0) process.exit(0)
...@@ -47,10 +47,10 @@ if (!existsSync(resolve(dir, '.next', 'BUILD_ID'))) { ...@@ -47,10 +47,10 @@ if (!existsSync(resolve(dir, '.next', 'BUILD_ID'))) {
process.exit(1) process.exit(1)
} }
srv.start(argv.port) srv.start(argv.port, argv.hostname)
.then(() => { .then(() => {
if (!process.env.NOW) { if (!process.env.NOW) {
console.log(`> Ready on http://localhost:${argv.port}`) console.log(`> Ready on http://${argv.hostname && typeof argv.hostname !== 'boolean' ? argv.hostname : 'localhost'}:${argv.port}`)
} }
}) })
.catch((err) => { .catch((err) => {
......
...@@ -123,14 +123,18 @@ export default class Server { ...@@ -123,14 +123,18 @@ export default class Server {
} }
} }
async start (port) { async start (port, hostname) {
await this.prepare() await this.prepare()
this.http = http.createServer(this.getRequestHandler()) this.http = http.createServer(this.getRequestHandler())
await new Promise((resolve, reject) => { await new Promise((resolve, reject) => {
// This code catches EADDRINUSE error if the port is already in use // This code catches EADDRINUSE error if the port is already in use
this.http.on('error', reject) this.http.on('error', reject)
this.http.on('listening', () => resolve()) this.http.on('listening', () => resolve())
this.http.listen(port) if (hostname && typeof hostname !== 'boolean') {
this.http.listen(port, hostname)
} else {
this.http.listen(port)
}
}) })
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment