Solved

node js inconsistent results

Posted on 2015-01-02
10
158 Views
Last Modified: 2015-01-22
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!
0
Comment
Question by:Isaac
  • 4
  • 4
  • 2
10 Comments
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 40528753
Because the first one is still running and takes precedence over the second one.
0
 
LVL 5

Author Comment

by:Isaac
ID: 40528770
Even though they are each in separate files.
How do I fix it?
0
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 40528779
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
 
LVL 5

Author Comment

by:Isaac
ID: 40528818
I did ctrl c and closed the browser then ran the 2nd program but still same results.
0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
ID: 40529572
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 5

Author Comment

by:Isaac
ID: 40529626
Windows 8
0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
ID: 40529630
Ok so open task manager and kill any node processes
0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
ID: 40529631
Or run the second example on a different port like 8081
0
 
LVL 42

Accepted Solution

by:
Rob Jurd, EE MVE earned 500 total points
ID: 40529663
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
 
LVL 5

Author Comment

by:Isaac
ID: 40564244
Thanks Rob! Sorry it took so long to get back.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

What is a Lightbox? A Lightbox is the effect you see when you click, for example, an image and the screen fades out and up pops the same image but in its full size dimensions. There are lots of Lightbox effects for jQuery. Problem is they are a…
Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

705 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

17 Experts available now in Live!

Get 1:1 Help Now