Skip to content

Commit

Permalink
hellonode: Handle SIGINT, add hostname and \n to response (#15)
Browse files Browse the repository at this point in the history
* Handle SIGINT exit, add hostname and \n to resp

- Ctrl+C on a Docker container sends SIGINT, which is not handled
  properly by default (see nodejs/node#4182). Added a signal handler.
- Adding trailing newline to response for curl-friendliness.
- Added log message indicating server is starting.
- Added hostname to indicate that the container.

Signed-off-by: Ahmet Alp Balkan <[email protected]>

* Address code review comments

Signed-off-by: Ahmet Alp Balkan <[email protected]>
  • Loading branch information
ahmetb authored Mar 29, 2017
1 parent 1bf091d commit 801b752
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions hellonode/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,23 @@
* limitations under the License.
*/

var http = require('http');
const http = require('http');
const os = require('os');

const port = process.env.PORT || 8080;

process.on('SIGINT', function() {
console.log('shutting down...');
process.exit(1);
});

var handleRequest = function(request, response) {
console.log('Received request for URL: ' + request.url);
console.log(`Received request for URL: ${request.url}`);
response.writeHead(200);
response.end('Hello, World!');
response.end(`Hello, World!\nHostname: ${os.hostname()}\n`);
};

var www = http.createServer(handleRequest);
www.listen(8080);
www.listen(port, () => {
console.log(`server listening on port ${port}`);
});

0 comments on commit 801b752

Please sign in to comment.