Reputation: 159
I am new to using node and I am trying to write a simple application (see below) however when I try to access it in my browser it simply loads forever and nothing ever happens. Does anyone know how to fix this? I looked at a few websites and other tutorials but nothing has worked so far. I was told to navigate to this link: http://localhost:8080/?data=put_some_text_here
Here is the code:
//include http module, add url module for parsing
var http = require("http"),
url = require("url");
//create the server
http.createServer(function(request, response) {
//attach listener on end event
request.on('end', function() {
//parse the request for arguements and store them in _get variable
//this function parses the url form request and returns obj representation
var _get = url.parse(request.url, true).query;
//write headers to the response
response.writeHead(200, {
'Content-Type': 'text/plain'
});
//send data and end response.
response.end('Here is your data: ' + _get['data']);
});
}).listen(8080);
Upvotes: 0
Views: 1288
Reputation: 1
You`re attach listener on end event but not fire this event.
//include http module, add url module for parsing
var http = require("http"),
url = require("url");
//create the server
http.createServer(function(request, response) {
//parse the request for arguements and store them in _get variable
//this function parses the url form request and returns obj representation
var _get = url.parse(request.url, true).query;
//write headers to the response
response.writeHead(200, {
'Content-Type': 'text/plain'
});
//send data and end response.
response.end('Here is your data: ' + _get['data']);
//attach listener on end event
request.on('end', function() {
//you can do something here
console.log('Event: Request End', request.url);
});
}).listen(8080);
Upvotes: 0
Reputation: 106698
You're not reading any data from the request, so your end
event handler will never get called, meaning you never end the response. If you don't care about the request data, you could simply do request.resume();
right before request.on('end', ...);
Upvotes: 1