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 Creating a Basic Template Engine in Node.js Binding Values

Michael Randall
PLUS
Michael Randall
Courses Plus Student 10,643 Points

Receiving "Moved Permanently" error message when searching for user profile after changing from https to http.

I am receiving a: "There was an error getting the profile for chalkers (Moved Permanently)" when I try to add a username to the end of my URL. The home file loads correctly, but adding a forward slash and username to the URL triggers the message.

I'm using http and not https in my code. Previously, I used https and was getting a workspace unavailable error. Another person recommended require http and that resolved that error. But now I can't get my profile, or anyone's profile.

At this point, there is too much code to copy and paste so I will only paste in app.js and router.js files

    var router = require("./router.js");
    var http = require('http');
    http.createServer(function (request, response){
      router.home(request, response);
      router.user(request, response);
    }).listen(8080);
    console.log('Server running at http://<workspace-url>/');
    var Profile = require("./profile.js");
    var renderer = require("./renderer.js");


    function home(request, response){
      if(request.url === "/"){
        response.writeHead(200, {'Content-Type': 'text/plain'});
        renderer.view("header", {}, response);
        renderer.view("search", {}, response);
        renderer.view("footer", {}, response);
        response.end();
      }
    }


    function user(request, response){
      var username = request.url.replace("/","");
      if(username.length > 0) {
        response.writeHead(200, {'Content-Type': 'text/plain'});
        renderer.view("header", {}, response);

        var studentProfile = new Profile(username);

        studentProfile.on("end", function(profileJSON){
          var values = {
            avatarUrl: profileJSON.gravatar_url,
            username: profileJSON.profile_name,
            badges: profileJSON.badges.length,
            javascriptPoints: profileJSON.points.JavaScript
          }
          renderer.view("profile", values, response);
          renderer.view("footer", {}, response);
          response.end();
        });

       studentProfile.on("error", function(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;

At least I know that the error part works. Any recommendations would be appreciated.

rydavim
rydavim
18,814 Points

I added a bit more code formatting to your post to make it easier to read. You can do this with the following markdown:

```language

your code here

```

3 Answers

rydavim
rydavim
18,814 Points

Your code works for me when I use it against my own files. This makes me think that the issue is perhaps in your profile.js file. The profile file is where it gets a bit tricky, as Treehouse switched from using http to using https with their profile api.

Try using https when getting the json, and then use http to handle the error. If you run into trouble, I'm happy to help troubleshoot your code if you post the profile file.

Happy coding!

Michael Randall
Michael Randall
Courses Plus Student 10,643 Points

Thanks MOD. It works now. Specifically, I had to add the following code to profile.js

  var https = require("https");

  var request = https.get("https://teamtreehouse.com/" + username + ".json", function(response) {
    var body = "";

    if (response.statusCode !== 200) {
        request.abort();
        //Status Code Error
        profileEmitter.emit("error", new Error("There was an error getting the profile for " + username + ". (" + http.STATUS_CODES[response.statusCode] + ")"));
    }

    //Read the data
    response.on('data', function (chunk) {
        body += chunk;
        profileEmitter.emit("data", chunk);
    });

I left http everywhere else and it worked

Jack Hegarty
Jack Hegarty
9,381 Points

I am not getting a Moved Permanatly but sort of the same error. When I go to add the "/chalkers" at 4:30 in the video my Workspace becomes unavailable???

Michael Randall
Michael Randall
Courses Plus Student 10,643 Points

Yes, that is the same thing. I've gotten responses about changing http to https and am changing those around, but still getting varying degrees of success. I will try to see what the other comments lead to and then update.

Zeljko Porobija
Zeljko Porobija
11,491 Points

Well, one just needs to replace all http occurencces with https and the things work fine.