'Receiving error message about syntax nodejs

I am new to Node js. I am following this tutorial online. I was trying to test the code but I get an error every time about line 1 syntax error and I got the code from the tutorial so I am not sure what the problem is? Can someone help me please? Thanks in advance

//Here is the code

     myfirst.js

     var http = require('http');

http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/html'});
  res.end('Hello World!');
}).listen(8080);


Solution 1:[1]

var http = require('http');

//create a server object:
http.createServer(function (req, res) {
  res.write('Hello World!'); //write a response to the client
  res.end(); //end the response
}).listen(8080); //the server object listens on port 8080

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Segun Adeniji