I have had to modify the code on basic Node.js file in order to make it work and I am wondering why?
this fails:
const server = http.createServer((req, res) => {
this works:
var server = http.createServer(function(req, res){
Error:
/my-app/tmp/hello2.js:6 var server = http.createServer((req, res) => { ^ SyntaxError: Unexpected token > at Module._compile (module.js:439:25) at Object.Module._extensions..js (module.js:474:10) at Module.load (module.js:356:32) at Function.Module._load (module.js:312:12) at Function.Module.runMain (module.js:497:10) at startup (node.js:119:16) at node.js:945:3
Complete code
const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;
//const server = http.createServer((req, res) => {
// above *wont work*?? below works
var server = http.createServer(function(req, res){
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World\n');
});
// server.listen(port, hostname, () => {
// above *wont work*?? below works
server.listen(port, hostname, function() {
console.log(`Server running at http://${hostname}:${port}/`);
});