Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Node.JS Can't get any output from the code

zak100

Coder
Hi,
I am running the following node.js code:

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


I found the code from: https://www.w3schools.com/nodejs/

I am executing using the command: $ node helloworld2.js
Somebody please guide me.

Zulfi.
 
Last edited by a moderator:
I see one error, function (, if it were function name( or function( with no space would help. X E.
That's not an error. In code, spaces are ignored (most of the time, with a few exceptions), so the space there does not change anything.

@zak100 Where are you expecting to get the output? In the command line or the browser? Have you opened http://localhost:8080 in the browser?
 
Just so we are clear what I was saying, there is a space between function and start parenthesis, change that to either no space or put a name of function. As @Johna said probably not needed though. I just know JS, not Node.js. X E.
 
That code works fine. Node.js just sits there listening to port 8080, and if you direct your browser to localhost:8080 you get the "Hello World". Spaces in the code of course play no part in this.
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom