node js inconsistent results

I was working through an exercise about node.js

The first exercise was an hello world exercise with the following code which worked.
var http = require('http');
http.createServer(function (request, response) {
	response.writeHead(200, {'Content-Type': 'text/plain'});
	response.end('Hello World from Node.js!\n');
	console.log('Handled request');
}).listen(8080, 'localhost');
console.log('Server running at http://localhost:8080/');

Open in new window


The code above renders "Hello World from Node.js!" in the browser using the command: node HellowWorld.js


The second exercise, which is to parse the querystring, had the following code but the results was the same as the one above:
var http = require('http');
var url = require('url');
http.createServer(function (request, response) {
	var url_parts = url.parse(request.url, true);
	response.writeHead(200, {'Content-Type': 'text/plain'});
	response.end('Hello '+url_parts.query.name + '!\n');
	console.log('Handled request from '+url_parts.query.name);
}).listen(8080, 'localhost');
console.log('Server running at http://localhost:8080/');

Open in new window


The above is supposed to render "Hello Joe!" in the browser if the query string is "http://localhost:8080/?name=Joe", but it does not.  It renders  "Hello World from Node.js!" in the browser using the command: node hello_joe.js

Any ideas why I am getting the same result for both?

Thanks!
LVL 5
IsaacSharePoint Client Side DeveloperAsked:
Who is Participating?
 
RobConnect With a Mentor Owner (Aidellio)Commented:
I.e.

var http = require('http');
var url = require('url');
http.createServer(function (request, response) {
      var url_parts = url.parse(request.url, true);
      response.writeHead(200, {'Content-Type': 'text/plain'});
      response.end('Hello '+url_parts.query.name + '!\n');
      console.log('Handled request from '+url_parts.query.name);
}).listen(8081, 'localhost');
console.log('Server running at http://localhost:8081/');
0
 
Dave BaldwinFixer of ProblemsCommented:
Because the first one is still running and takes precedence over the second one.
0
 
IsaacSharePoint Client Side DeveloperAuthor Commented:
Even though they are each in separate files.
How do I fix it?
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
Dave BaldwinFixer of ProblemsCommented:
I don't know yet.  I had to put them on separate ports and an IP address, not localhost, to get them to run.  But using Ctl-C to get out of node.js killed the programs so they were never both running at the same time.
0
 
IsaacSharePoint Client Side DeveloperAuthor Commented:
I did ctrl c and closed the browser then ran the 2nd program but still same results.
0
 
RobOwner (Aidellio)Commented:
Dave's got it right, you're still running the first node process. How are you running then at the command line?  Running on Windows or Linux?
You'll need to kill the first node process
0
 
IsaacSharePoint Client Side DeveloperAuthor Commented:
Windows 8
0
 
RobOwner (Aidellio)Commented:
Ok so open task manager and kill any node processes
0
 
RobOwner (Aidellio)Commented:
Or run the second example on a different port like 8081
0
 
IsaacSharePoint Client Side DeveloperAuthor Commented:
Thanks Rob! Sorry it took so long to get back.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.