Skip to content

Commit

Permalink
prevent excess server boots via server option
Browse files Browse the repository at this point in the history
- Closes #33
  • Loading branch information
lukeed committed Apr 22, 2018
1 parent 46285f9 commit 276056c
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 2 deletions.
3 changes: 2 additions & 1 deletion packages/polka/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ class Polka extends Router {
this.wares = [];
this.bwares = {};
this.parse = parseurl;
this.server = opts.server;
this.handler = this.handler.bind(this);
this.server = http.createServer(this.handler);
this.onError = opts.onError || onError; // catch-all handler
this.onNoMatch = opts.onNoMatch || this.onError.bind(null, { code:404 });
}
Expand All @@ -60,6 +60,7 @@ class Polka extends Router {
}

listen(port, hostname) {
(this.server = this.server || http.createServer()).on('request', this.handler);
return new Promise((res, rej) => {
this.server.listen(port, hostname, err => err ? rej(err) : res());
});
Expand Down
2 changes: 1 addition & 1 deletion tests/polka.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ test('polka::internals', t => {
t.isObject(app.bwares, 'app.bwares is an object');
t.isEmpty(app.bwares, 'app.bwares is empty');

t.ok(app.server instanceof http.Server, 'app.server is an HTTP server');
t.is(app.server, undefined, 'app.server is undefined by default (pre-listen)');

t.isFunction(app.onError, 'app.onError is a function');
t.isFunction(app.onNoMatch, 'app.onNoMatch is a function');
Expand Down

0 comments on commit 276056c

Please sign in to comment.