Skip to content

Commit 2c4c239

Browse files
authored
Feature: add onListening hook (#69)
* Add onListening option, call in server callback * Add test for verifying onListening executed within timeout * Add onListening to documentation * Update onListening docs showing how to access other options * Format
1 parent c79a902 commit 2c4c239

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

README.md

+9
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,15 @@ serve({
9494
mimeTypes: {
9595
'application/javascript': ['js_commonjs-proxy']
9696
}
97+
98+
// execute function after server has begun listening
99+
onListening: function (server) {
100+
const address = server.getAddress()
101+
const host = address.host === '::' ? 'localhost' : address.host
102+
// by using a bound function, we can access options as `this`
103+
const protocol = this.https ? 'https' : 'http'
104+
console.log(`Server listening at ${protocol}://${host}:${address.port}/`)
105+
}
97106
})
98107
```
99108

src/index.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ function serve (options = { contentBase: '' }) {
2121
options.headers = options.headers || {}
2222
options.https = options.https || false
2323
options.openPage = options.openPage || ''
24+
options.onListening = options.onListening || function noop () { }
2425
mime.default_type = 'text/plain'
2526

2627
if (options.mimeTypes) {
@@ -74,9 +75,15 @@ function serve (options = { contentBase: '' }) {
7475

7576
// If HTTPS options are available, create an HTTPS server
7677
if (options.https) {
77-
server = createHttpsServer(options.https, requestListener).listen(options.port, options.host)
78+
server = createHttpsServer(options.https, requestListener)
79+
server.listen(options.port, options.host, () => {
80+
options.onListening(server)
81+
})
7882
} else {
79-
server = createServer(requestListener).listen(options.port, options.host)
83+
server = createServer(requestListener)
84+
server.listen(options.port, options.host, () => {
85+
options.onListening(server)
86+
})
8087
}
8188

8289
// Assemble url for error and info messages
@@ -178,6 +185,7 @@ export default serve
178185
* @property {string|boolean} [historyApiFallback] Path to fallback page. Set to `true` to return index.html (200) instead of error page (404)
179186
* @property {string} [host='localhost'] Server host (default: `'localhost'`)
180187
* @property {number} [port=10001] Server port (default: `10001`)
188+
* @property {function} [onListening] Execute a function when server starts listening for connections on a port
181189
* @property {ServeOptionsHttps} [https=false] By default server will be served over HTTP (https: `false`). It can optionally be served over HTTPS
182190
* @property {{[header:string]: string}} [headers] Set headers
183191
*/

test/rollup.config.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
import serve from '../src/index.js'
22

3+
const testOnListening = () => {
4+
const timeout = 3
5+
const timer = setTimeout(() => {
6+
const msg = `onListening was not called within ${timeout}s`
7+
console.error(msg)
8+
throw new Error(msg)
9+
}, timeout * 1000)
10+
return (server) => {
11+
clearTimeout(timer)
12+
console.log('onListening works', server.address())
13+
}
14+
}
15+
316
export default {
417
input: 'entry.js',
518
output: {
@@ -11,7 +24,8 @@ export default {
1124
open: true,
1225
openPage: '/frames.html',
1326
historyApiFallback: '/fallback.html',
14-
contentBase: ['.', 'base1', 'base2']
27+
contentBase: ['.', 'base1', 'base2'],
28+
onListening: testOnListening(),
1529
})
1630
]
1731
}

0 commit comments

Comments
 (0)