Skip to content

Commit c79a902

Browse files
authored
Merge pull request #60 from jaeh/eaddrinuse
src/index: add error message if port is in use
2 parents 510c69c + 662e882 commit c79a902

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

src/index.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,19 @@ function serve (options = { contentBase: '' }) {
7979
server = createServer(requestListener).listen(options.port, options.host)
8080
}
8181

82+
// Assemble url for error and info messages
83+
const url = (options.https ? 'https' : 'http') + '://' + (options.host || 'localhost') + ':' + options.port
84+
85+
// Handle common server errors
86+
server.on('error', e => {
87+
if (e.code === 'EADDRINUSE') {
88+
console.error(url + ' is in use, either stop the other server or use a different port.')
89+
process.exit()
90+
} else {
91+
throw e
92+
}
93+
})
94+
8295
let first = true
8396

8497
return {
@@ -88,7 +101,6 @@ function serve (options = { contentBase: '' }) {
88101
first = false
89102

90103
// Log which url to visit
91-
const url = (options.https ? 'https' : 'http') + '://' + (options.host || 'localhost') + ':' + options.port
92104
if (options.verbose !== false) {
93105
options.contentBase.forEach(base => {
94106
console.log(green(url) + ' -> ' + resolve(base))

0 commit comments

Comments
 (0)