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 Reading from Files

Karalyn Heath
Karalyn Heath
18,033 Points

Getting Workspace Unavailable instead of JS error message: Build a Simple Dynamic Site with Node.js

The home page works fine and I get the expected result when I enter an available username. However, when an unknown name is added to the url the browser window says 'Workspace Unavailable' and the console shows this:

events.js:141                                                                                                            
      throw er; // Unhandled 'error' event                                                                               
      ^                                                                                                                  

Error: write after end                                                                                                   
    at ServerResponse.OutgoingMessage.write (_http                                                                       
    at Object.view (/home/treehouse/workspace/rend                                                                       
    at Profile.<anonymous> (/home/treehouse/worksp                                                                       
    at emitOne (events.js:77:13)                                                                                         
    at Profile.emit (events.js:169:7)                                                                                    
    at IncomingMessage.<anonymous> (/home/treehous                                                                       
    at emitNone (events.js:72:20)                                                                                        
    at IncomingMessage.emit (events.js:166:7)                                                                            
    at endReadableNT (_stream_readable.js:905:12)                                                                        
    at nextTickCallbackWith2Args (node.js:442:9)  

I don't have an events.js file. Is this a workspace error?

Here's my router.js file:

var Profile = require("./profile.js");
var renderer = require("./renderer.js");

//Handle the HTTP route GET / and POST / i.e. Home
function home(request, response) {
  //If url == "/" && GET
  if(request.url === "/") {
    //show search
    response.writeHead(200, {'Content-Type': 'text/plain'});
    renderer.view("header", {}, response);
    renderer.view("search", {}, response);
    renderer.view("footer", {}, response);
    response.end();
  }
  //If url == "" && POST
    //redirect to /:username
}

//Handle HTTP route GET / :username i.e. /chalkers
function user(request, response) {
  //if url== "/...."
  var username = request.url.replace("/", "");
  if(username.length > 0) {
    response.writeHead(200, {'Content-Type': 'text/plain'});
    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 which we need
      var values = {
        avatarUrl: profileJSON.gravatar_url,
        username: profileJSON.profile_name,
        badges: profileJSON.badges.length,
        javacriptPoints: profileJSON.points.JavaScript
      }
      //Simple response
      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;
Karalyn Heath
Karalyn Heath
18,033 Points

I restarted workspaces, tried again and still not working. But now, after posting in the forum, it decided to work. sigh