Solved

Node JS Won't Print Hello World

Posted on 2015-02-20
8
156 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 43

Expert Comment

by:Rob
ID: 40622543
you should be accessing: servername.com:8008 so drop the helloworld.js
0
 
LVL 43

Expert Comment

by:Rob
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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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
 

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 43

Accepted Solution

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

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

It is becoming increasingly popular to have a front-page slider on a web site. Nearly every TV website,  magazine or online news has one on their site, and even some e-commerce sites have one. Today you can use sliders with Joomla, WordPress or …
There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

809 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