1
0
Fork 0
mirror of https://github.com/terribleplan/next.js.git synced 2024-01-19 02:48:18 +00:00

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
This commit is contained in:
Jesse Hattabaugh 2017-02-12 03:26:10 -08:00 committed by Tim Neutkens
parent 19f1125520
commit c4a22abb4c
2 changed files with 11 additions and 7 deletions

View file

@ -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) {
@ -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)
@ -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) => {

View file

@ -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())
if (hostname && typeof hostname !== 'boolean') {
this.http.listen(port, hostname)
} else {
this.http.listen(port)
}
})
}