Skip to content
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

Merged
merged 2 commits into from
Mar 29, 2017

Conversation

ahmetb
Copy link
Contributor

@ahmetb ahmetb commented Mar 29, 2017

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

- 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]>
Copy link

@MylesBorins MylesBorins left a 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


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');

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

@@ -15,11 +15,19 @@
*/

var http = require('http');

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

@@ -15,11 +15,19 @@
*/

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

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;


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

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?

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

var handleRequest = function(request, response) {
console.log('Received request for URL: ' + request.url);

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}`);

var www = http.createServer(handleRequest);
console.log('server listening on port 8080');

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;

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.

Copy link
Contributor Author

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.

Copy link

@MylesBorins MylesBorins left a 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

@ahmetb ahmetb merged commit 801b752 into GoogleCloudPlatform:master Mar 29, 2017
@ahmetb ahmetb deleted the hellonode branch March 29, 2017 22:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants