Solved

Node JS Won't Print Hello World

Posted on 2015-02-20
8
148 Views
Last Modified: 2015-02-22
I'm using node.js on a linux server. node js located at /usr/bin/node.  My helloworld.js  looks like this:

var http = require('http');

http.createServer(function (request, response) {
    response.writeHead(200, {'Content-Type': 'text/plain'});
    response.end('Hello World\n');
}).listen(8008);
;
console.log('Server started');

when executing  /usr/bin/node helloworld.js,  "Server started" prints in the console but what appears in the browser (servername.com:8008/helloworld.js) is the code block itself , not "Hello World" ???  I'm not able to find any info on this type of error.   Code worked just fine in wamp on my windows box.
0
Comment
Question by:kevbob650
  • 3
  • 3
  • 2
8 Comments
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
ID: 40622543
you should be accessing: servername.com:8008 so drop the helloworld.js
0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
ID: 40622551
When you start your server you run: node helloworld.js
0
 

Author Comment

by:kevbob650
ID: 40624521
now I'm getting this error (or didn't see it the first time?):

$ node helloworld.js
Server started

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: listen EADDRINUSE
    at errnoException (net.js:904:11)
    at Server._listen2 (net.js:1042:14)
    at listen (net.js:1064:10)
    at Server.listen (net.js:1138:5)
    at Object.<anonymous> (/mypath/helloworld.js:6:4)
    at Module._compile (module.js:456:26)
    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)

I can't find documentation to understand the error message.   Thanks for first response!
0
 
LVL 25

Expert Comment

by:clockwatcher
ID: 40624535
That error means the port that you're trying to bind to is already in use.  Most likely because you have another instance of your helloworld.js node program still running.    Try a:

ps -Af | grep helloworld

To check.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:kevbob650
ID: 40624550
Sorry, I'm a linux dummy, here's the results of that command, no idea what it means:
$ ps -Af | grep helloworld
13164 12422  0 19:55 pts/1    00:00:00 grep helloworld
0
 
LVL 25

Assisted Solution

by:clockwatcher
clockwatcher earned 250 total points
ID: 40624568
From that output, it doesn't look like it's still running.   I would try starting it again and see if you still get that error.  

If you do, you can check what process is running on port 8008 with:

sudo netstat -anp | grep 8008

If you get any output from the above, the last column will tell you want process number is using the port.  E.g.,

mark@blue:~/node$ sudo netstat -anp | grep 8008
tcp        0      0 0.0.0.0:8008            0.0.0.0:*               LISTEN      9124/node

The above indicates that process 9124 (node) is running listening on port 8008.
0
 

Author Comment

by:kevbob650
ID: 40624595
looks like it's now:   tcp        0      0 1XX.XX.XX.XX:8008          0.0.0.0:*                   LISTEN      -
0
 
LVL 42

Accepted Solution

by:
Rob Jurd, EE MVE earned 250 total points
ID: 40624608
So for testing just ruin your node app on another port, like 8009
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
WPF issue with Trigger 2 96
Not needed 13 94
Adjust the position 3 49
ICACL switch help. How to include current folder 2 37
I hope you'll find this tutorial useful and interesting. So let's try to extend Tcl with a new package.  For anyone more deeply interested please check out the book "Practical Programming in Tcl and Tk". It's really one of the best written books abo…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now