Solved

Node JS Won't Print Hello World

Posted on 2015-02-20
8
162 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
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

Tutorials alone can't teach real engineering

So we built better training tools.

-Hands-on Labs
-Instructor Mentoring
-Scenario-Based Tests
-Dedicated Cloud Servers

All at your fingertips. What are you waiting for?

Question has a verified solution.

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

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 threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

617 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