Skip to content
This repository has been archived by the owner on Mar 10, 2020. It is now read-only.

refactor: http-server util file #565

Merged
merged 1 commit into from
Dec 2, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions src/utils/echo-http-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,11 @@ module.exports.createServer = () => {

const server = http.createServer(handler)

server.start = (opts, cb) => {
if (typeof opts === 'function') {
cb = opts
opts = {}
}
return server.listen(Object.assign({ port: defaultPort, host: '127.0.0.1' }, opts), cb)
}
server.start = (opts) => new Promise(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should handle errors while starting up:

server.start = (opts) => new Promise((resolve, reject) => {
  server.once('error', reject)
  server.listen(Object.assign({ port: defaultPort, host: '127.0.0.1' }, opts), () => {
    server.removeListener('error', reject)
    resolve()
  })
})

Same thing for shutting down.

(resolve) => server.listen(Object.assign({ port: defaultPort, host: '127.0.0.1' }, opts), resolve)
)

server.stop = (cb) => server.close(cb)
server.stop = () => new Promise((resolve) => server.close(resolve))

return server
}
Expand Down