Skip to content

Commit

Permalink
adding hostname argument to CLI (vercel#1017)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
jessehattabaugh authored and timneutkens committed Feb 12, 2017
1 parent 19f1125 commit c4a22ab
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
10 changes: 5 additions & 5 deletions bin/next-start
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 10,11 @@ process.env.NODE_ENV = process.env.NODE_ENV || 'production'
const argv = parseArgs(process.argv.slice(2), {
alias: {
h: 'help',
H: 'hostname',
p: 'port'
},
boolean: ['h'],
default: {
p: 3000
}
default: { p: 3000 }
})

if (argv.help) {
Expand All @@ -33,6 32,7 @@ if (argv.help) {
Options
--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
`)
process.exit(0)
Expand All @@ -47,10 47,10 @@ if (!existsSync(resolve(dir, '.next', 'BUILD_ID'))) {
process.exit(1)
}

srv.start(argv.port)
srv.start(argv.port, argv.hostname)
.then(() => {
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) => {
Expand Down
8 changes: 6 additions & 2 deletions server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 123,18 @@ export default class Server {
}
}

async start (port) {
async start (port, hostname) {
await this.prepare()
this.http = http.createServer(this.getRequestHandler())
await new Promise((resolve, reject) => {
// This code catches EADDRINUSE error if the port is already in use
this.http.on('error', reject)
this.http.on('listening', () => resolve())
this.http.listen(port)
if (hostname && typeof hostname !== 'boolean') {
this.http.listen(port, hostname)
} else {
this.http.listen(port)
}
})
}

Expand Down

0 comments on commit c4a22ab

Please sign in to comment.