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 Build a Simple Dynamic Site with Node.js HTTP Methods and Headers Dealing with the POST Body

janeporter
PLUS
janeporter
Courses Plus Student 23,471 Points

bad gateway error on workspaces preview

i keep getting a bad gateway error (code 501) on previews for workspaces now. This is the address that is getting the error:

http://port-3000-hkji8zsut5.treehouse-app.com/

why is that and how can i fix it?

5 Answers

josue exhume
josue exhume
20,981 Points

make sure your check for the get is

if(request.method.toLowerCase() === "get"){

and not

if(request.method.toLowerCase === "get"){

because i had the same error as you and that turned out to be my problem. Hope that helped =]

janeporter
PLUS
janeporter
Courses Plus Student 23,471 Points

i have that, and i'm still getting the error. the workspace page is stating 'workspace unavailable: this is a preview link for a Treehouse Workspace that is not currently active.'

then i see these errors when i do an inspect element:

Failed to load resource: the server responded with a status of 502 (Bad Gateway) http://port-3000-hkji8zsut5.treehouse-app.com/ Failed to load resource: the server responded with a status of 502 (Bad Gateway) Failed to clear temp storage: It was determined that certain files are unsafe for access within a Web application, or that too many calls are being made on file resources. SecurityError

josue exhume
josue exhume
20,981 Points

im not to sure why that happened, but i looked at what the 502 error was, turn out its "The server cannot process the request due to a high load (whether HTTP servicing or other requests)." so im guessing it has to do with the request were doing in the home function.

but take a look at my router.js file check to see if you have anything different

var Profile = require("./profile.js");
var renderer = require('./renderer.js');
var querystring = require("querystring");
var commonHeaders ={'Content-Type': 'text/html'};


//Handle HTTP route GET / and POST / I.E Home
function home (request, response) {
  // if url =="/" && GET
  if(request.url === "/"){
    if(request.method.toLowerCase() === "get"){
    //show search field
    response.writeHead(200, commonHeaders);
    renderer.view("header",{},response);
    renderer.view("search",{},response);
    renderer.view("footer",{},response);
    response.end();
    } else {
  // if url == "/" && POST

      //get the post data from body
      request.on("data",function(postBody){
        //extract the username
      var query = querystring.parse(postBody.toString());
        //redirect to username
        response.writeHead(303, {"Location": "/" + query.username});
        response.end();

      });


    }
  }

}


//Handle HTTP route GET /:username i.e /josueexhume
function user (request, response) {

  // if url =="/.."
  var username = request.url.replace("/", "");

  if (username.length > 0){
   response.writeHead(200, commonHeaders);
    renderer.view("header", {},response);


    //get json from Treehouse
    var studentProfile = new Profile(username);
      // on "end" 
    studentProfile.on("end", function(profileJSON){
        //show profile    

        //store the values we need
        var values = {
          avatarUrl: profileJSON.gravatar_url,
          username: profileJSON.profile_name,
          badges: profileJSON.badges.length,
          javaScript: profileJSON.points.JavaScript
        }
        //simple reponse
        renderer.view("profile", values, response);
        renderer.view("footer",{},response);
        response.end();
    });

    //on error 
    studentProfile.on("error", function(error){
    //show error
    renderer.view("error", {errorMessage: error.message}, response);
    renderer.view("search",{},response);
    renderer.view("footer",{},response);
    response.end();
    });

  }
}

module.exports.home = home;
module.exports.user = user;

heres my renderer.js file also

var fs = require('fs');

function mergeValues(values, content){
  //cycle over the keys
  for(var key in values){
    content = content.replace("{{"+ key +"}}", values[key]);
  }
  //replace all{{key}} with the vale from the values object

  //return merged content
  return content;
}

function view (templateName, values, response) {
var fileContent = fs.readFileSync('./views/'+templateName+ '.html', {encoding: "utf8"});
  //read from the template file
  fileContent = mergeValues(values,fileContent);
  //insert values in to the content

  //write out to the response
  response.write(fileContent);

}

module.exports.view = view;

hope this can help in any way.

an example of streaming

var readable = getReadableStreamSomehow();
readable.on('data', (chunk) => {
  console.log('got %d bytes of data', chunk.length);
});

When a chunk of data can be read from the stream, it will emit a 'readable' event.

In some cases, listening for a 'readable' event will cause some data to be read into the internal buffer from the underlying system, if it hadn't already.

I guess this 'readable' is slightly different from 'data' it is the leftover that 'data' doesn't take care of gets streamed.

var readable = getReadableStreamSomehow();
readable.on('readable', () => {
  // there is some data to read now
});