Skip to content

Instantly share code, notes, and snippets.

@conorhastings
Last active March 23, 2016 02:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save conorhastings/97c2db126c7d36a3a306 to your computer and use it in GitHub Desktop.
Save conorhastings/97c2db126c7d36a3a306 to your computer and use it in GitHub Desktop.
Node allows use of const (but not let) outside of strict mode but does not seem to respect block scoping
/*
* In this version without use strict declaration 'SyntaxError: Identifier 'text' has already been declared' is given
*/
const http = require('http');
const hostname = 'localhost';
const port = 2001;
http.createServer((req, res) => {
if (req.url === '/favicon.ico') {
return;
}
const query = req.url.includes("?") && req.url.split('?')[1].split('=');
res.writeHead(200, { 'Content-Type': 'text/plain' });
if (query[1] === 'true') {
const text = 'Hello World';
res.end(text);
}
else {
const text = 'Goodbye World';
res.end(text);
}
}).listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
'use strict';
const http = require('http');
const hostname = 'localhost';
const port = 2001;
http.createServer((req, res) => {
if (req.url === '/favicon.ico') {
return;
}
const query = req.url.includes("?") && req.url.split('?')[1].split('=');
res.writeHead(200, { 'Content-Type': 'text/plain' });
if (query[1] === 'true') {
const text = 'Hello World';
res.end(text);
}
else {
const text = 'Goodbye World';
res.end(text);
}
}).listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment