Skip to content

Commit

Permalink
Merge pull request #11 from vinjoel/close-server-on-termination
Browse files Browse the repository at this point in the history
Close the server when a termination signal is encountered
  • Loading branch information
thgh authored Aug 16, 2017
2 parents eb41104 + 8dd57bc commit 4b5b265
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default function serve (options = { contentBase: '' }) {

mime.default_type = 'text/plain'

createServer(function (request, response) {
const server = createServer(function (request, response) {
// Remove querystring
const urlPath = decodeURI(request.url.split('?')[0])

Expand Down Expand Up @@ -59,6 +59,8 @@ export default function serve (options = { contentBase: '' }) {
})
}).listen(options.port)

closeServerOnTermination(server);

var running = options.verbose === false

return {
Expand Down Expand Up @@ -116,3 +118,13 @@ function found (response, filePath, content) {
function green (text) {
return '\u001b[1m\u001b[32m' + text + '\u001b[39m\u001b[22m'
}

function closeServerOnTermination(server) {
const terminationSignals = ['SIGINT', 'SIGTERM'];
terminationSignals.forEach((signal) => {
process.on(signal, () => {
server.close();
process.exit();
})
})
}

0 comments on commit 4b5b265

Please sign in to comment.