Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Data event never fires

I had performed all the steps in the video. Only got stuck when it reads the Posted data by form. Whenever I submit the search form, the page keeps loading indefinitely. Because data event never fired.

I have found this article from internet, on How do properly read post data? and also shown the possibility that data event triggered before function is binded to data event. That's why data event never triggered function.

Please recommend a correct way to bind subscribe to data event of request parameter.

2 Answers

Steven Parker
Steven Parker
243,318 Points

Without seeing your code or a link to the video I'm mostly guessing, but the obvious solution seems to be making sure the event handler is bound before transmitting the form that could cause the event. Is there something about the scenario that would prevent this?

If this doesn't happen to resolve the issue, perhaps you could provide a link to the course and video, plus make a snapshot of your workspace and post the link to it here.

Link to Video:

Dealing with the POST Body

Here is the code.

app.js:

var http= require("http");
var router=require("./router.js");
var querystring=require("querystring");

var server=http.createServer();

server.on('request',function(request, response){


  router.home(request, response);
  router.user(request, response);

  //response.end("<title>Hello World</title><div style='background-color:cyan;color:#fff;'><h1>Working from Node</h1></div>\n");
}).listen(3000,'localhost');

console.log("Server running at http://localhost:3000/");

router.js:

var fs=require("fs");

function homeRoute(request, response)
{
  if(request.url === "/" && request.method.toLowerCase()=="get")
  {
    response.statusCode=200;
    response.setHeader('Content-Type', 'text/html');
    var homeContent= fs.readFileSync("./views/home.html",{encoding:"utf8"});
    response.end(homeContent);

  }
  else if(request.url === "/" && request.method.toLowerCase()=="post")
  {

    var thebody='';
    request.on("data",function(postBody){
      thebody+=postBody.toString();
      //response.end(postBody.toString());
      //var homeContent= fs.readFileSync("./views/home.html",{encoding:"utf8"});
      //response.end(homeContent);
    }).on("end",function(){
      //console.log("Oeee");
      console.log(thebody);
      response.statusCode=200;
      response.setHeader('Content-Type', 'text/html');
      response.write(thebody);
      response.end();
      //var homeContent= fs.readFileSync("./views/home.html",{encoding:"utf8"});
      //response.end(homeContent);
    });
    //response.end();
  }
}

function userRoute(request, response)
{
  var username=request.url.replace("/","");
  if(username.length>0 && request.method=="GET")
  {
    response.statusCode=200;
    response.setHeader('Content-Type', 'text/html');
    response.end("This is user info page");
  }
}

module.exports.home=homeRoute;
module.exports.user=userRoute;

Workspace Link:

https://w.trhou.se/8lx785bg15

Again Video Link:

Dealing with the POST Body