Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add new option autoPort #84

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -14,3 +14,5 @@ node_modules
dist/*
!dist/favicon.ico
test/dest.js
package-lock.json
yarn.lock
3 changes: 3 additions & 0 deletions README.md
Expand Up @@ -77,6 +77,9 @@ serve({
host: 'localhost',
port: 10001,

// Avaliable auto change port when conflict.
autoPort: true,

// By default server will be served over HTTP (https: false). It can optionally be served over HTTPS
https: {
key: fs.readFileSync('/path/to/server.key'),
Expand Down
29 changes: 23 additions & 6 deletions src/index.js
Expand Up @@ -18,6 +18,7 @@ function serve (options = { contentBase: '' }) {
}
options.contentBase = Array.isArray(options.contentBase) ? options.contentBase : [options.contentBase || '']
options.port = options.port || 10001
options.autoPort = typeof (options.autoPort) === 'undefined' ? true : options.autoPort
options.headers = options.headers || {}
options.https = options.https || false
options.openPage = options.openPage || ''
Expand Down Expand Up @@ -77,16 +78,29 @@ function serve (options = { contentBase: '' }) {
server = options.https
? createHttpsServer(options.https, requestListener)
: createServer(requestListener)
server.listen(options.port, options.host, () => options.onListening(server))

let port = options.port
const startByPort = () => {
server.listen(port, options.host, () => options.onListening(server))
}
startByPort()

// Assemble url for error and info messages
const url = (options.https ? 'https' : 'http') + '://' + (options.host || 'localhost') + ':' + options.port
const getUrl = () => {
return (options.https ? 'https' : 'http') + '://' + (options.host || 'localhost') + ':' + port
}

// Handle common server errors
server.on('error', e => {
if (e.code === 'EADDRINUSE') {
console.error(url + ' is in use, either stop the other server or use a different port.')
process.exit()
if (options.autoPort) {
console.error(getUrl() + ' is in use, change to another port.')
port++
startByPort()
} else {
console.error(getUrl() + ' is in use, either stop the other server or use a different port.')
process.exit()
}
} else {
throw e
}
Expand All @@ -102,8 +116,10 @@ function serve (options = { contentBase: '' }) {

// Log which url to visit
if (options.verbose !== false) {
console.log('')
console.log('App running at:')
options.contentBase.forEach(base => {
console.log(green(url) + ' -> ' + resolve(base))
console.log('- ' + green(getUrl()) + ' -> ' + resolve(base))
})
}

Expand All @@ -112,7 +128,7 @@ function serve (options = { contentBase: '' }) {
if (/https?:\/\/.+/.test(options.openPage)) {
opener(options.openPage)
} else {
opener(url + options.openPage)
opener(getUrl() + options.openPage)
}
}
}
Expand Down Expand Up @@ -178,6 +194,7 @@ export default serve
* @property {string|boolean} [historyApiFallback] Path to fallback page. Set to `true` to return index.html (200) instead of error page (404)
* @property {string} [host='localhost'] Server host (default: `'localhost'`)
* @property {number} [port=10001] Server port (default: `10001`)
* @property {boolean} [autoPort=true] Server port (default: `10001`)
* @property {function} [onListening] Execute a function when server starts listening for connections on a port
* @property {ServeOptionsHttps} [https=false] By default server will be served over HTTP (https: `false`). It can optionally be served over HTTPS
* @property {{[header:string]: string}} [headers] Set headers
Expand Down