Solved

Node JS Won't Print Hello World

Posted on 2015-02-20
8
147 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

759 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

19 Experts available now in Live!

Get 1:1 Help Now