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

write after end in search functionality

I am trying to work on the search functionality that redirects the post request to the /username page. However, my example does not seem to work like the video. I made some tweaks

                     if (request.url === "/") {
        if (request.method.toLowerCase() === "get"){
            response.writeHead(200, commonHeaders);
            renderer.view("header",{},response);
            renderer.view("search",{}, response);
            renderer.view("footer", {}, response);
            response.end();             
        } else {
            request.on("data", function(postBody) {
                var query = qs.parse(postBody.toString());
                response.writeHead(303, {"Location": "/" + query.username });
                console.log("write head");
                response.end();
            }); 
        }

The problem is, somehow the response.end() from the "get" if statement is affecting the else statement, which doesn't make sense to me. If I remove the first response.end(), I can then use the search functionality.

So far, I've tried removing both response.end() and adding at the end,

    request.on("end", function(){
        response.end();
    });

this works in a way, but it causes the page to constantly have the loading icon on the page.

suggestions and lessons are greatly appreciated.

my problem was adding in a link tag for a stylesheet. it causes an extra request for the style sheet causing my error. I currently just catch the styles request ahead of the home and user route and load in the css. It appears to solve the problem.