-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
hellonode: Handle SIGINT, add hostname and \n to response #15
Conversation
- 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]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Put in a handful of comments. None of this is 100% necessary, but it is slightly more modern
hellonode/server.js
Outdated
|
||
var handleRequest = function(request, response) { | ||
console.log('Received request for URL: ' + request.url); | ||
response.writeHead(200); | ||
response.end('Hello, World!'); | ||
response.end('Hello, World!\nHostname: ' + os.hostname() + '\n'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you could use a template string here
`Hello, World!\nHostname: ${os.hostname()}\n`
you could also potentially cache the hostname above to avoid the function calls on each request, that is perhaps overkill though
hellonode/server.js
Outdated
@@ -15,11 +15,19 @@ | |||
*/ | |||
|
|||
var http = require('http'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
modern node is using const
rather than var for requires
hellonode/server.js
Outdated
@@ -15,11 +15,19 @@ | |||
*/ | |||
|
|||
var http = require('http'); | |||
var os = require('os'); | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it might be nice to declare the port here, and give an option to declare it via env_var
const port = process.env.NODE_PORT || 8080;
hellonode/server.js
Outdated
|
||
process.on('SIGINT', function() { | ||
console.log('shutting down...'); | ||
process.exit(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if you SIGINT should the process exit cleanly?
hellonode/server.js
Outdated
process.on('SIGINT', function() { | ||
console.log('shutting down...'); | ||
process.exit(); | ||
}); | ||
|
||
var handleRequest = function(request, response) { | ||
console.log('Received request for URL: ' + request.url); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can be done with template string
console.log(`Received request for URL: ${request.url}`);
hellonode/server.js
Outdated
var www = http.createServer(handleRequest); | ||
console.log('server listening on port 8080'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you might want to put this message inside of a callback to the listen call, to ensure it doesn't print until the server is listening
www.listen(port, ( ) => {
console.log(`server is listening on port ${port}`);
});
Signed-off-by: Ahmet Alp Balkan <[email protected]>
const http = require('http'); | ||
const os = require('os'); | ||
|
||
const port = process.env.PORT || 8080; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: do you want to make this more unique than PORT? possible to already exist on system.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@MylesBorins kind of a convention, so I'll keep it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tiny nit re: env var, but otherwise LGTM
properly by default (see Node v5.x.x does not exit on Ctrl-C in Docker container nodejs/node#4182). Added a signal handler.
Signed-off-by: Ahmet Alp Balkan [email protected]