Keren Keren - 5 months ago 26
Node.js Question

Node.js emits runtime error when listen to port 9999

I'm completely new to nodeJs, and just to learn step by step. I managed to download, run nodeJs via the CMD and tested by creating file, all works fine. The problem now is when I want to tell nodeJs to display the output on the browser instead. I simply followed the tutorial but I'm getting runtime error. I suspect the port is somewhat wrong. Which port no I should use here, is it my localhost port which is 80, I tried it to no avail.

Here's the error message:
enter image description here

Referred here:Using node.js as a simple web server

All I want is to be able to see the output in the browser via nodeJs. I think the port 9999 is meant for tcp connection as I'm learning for real-time app.

Answer

You're not using NodeJS to run your code. To run your code using NodeJS, run them with node:

node c:\projects\test\helloweb.js

The reason what you tried didn't work is that typing c:\projects\test\helloweb.js in cmd.exe and pressing Enter will not run the code in a browser, or in NodeJS. It will try to run it using whatever application you have associated with .js files. The Windows default, which is what your computer is using, is to use Windows Script Host (as you can see in your error message). WSH provides a runtime environment that's quite different from NodeJS and quite different from a browser.

Comments