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 Handling Routes in Node.js Populating User Information

Steve Anderson
Steve Anderson
4,850 Points

ERR_INCOMPLETE_CHUNK is an error I keep getting from treehouse.

This page isn’t working

port-3000-6cyk143ym6.treehouse-app.com unexpectedly closed the connection. ERR_INCOMPLETE_CHUNKED_ENCODING

This is before I finished the error handling function. This message appears when I try to load the user data. Is this a treehouse issue or on my side?

Brendan Albert
Brendan Albert
32,954 Points

Hi Steve ? Unless you already figured it out, I would be happy to look over your code if you post it.

I should mention, I followed along using Atom as my text editor and ran the server on my own computer.

I find it to be a great exercise figuring out how to get a node server up and running without relying on Workspaces.

Steve Anderson
Steve Anderson
4,850 Points

This is the code from the router file. I'm thankful for any help you can provide.

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

//Handle 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'});
    response.write('Header\n');
    response.write('Search\n');
    response.end('Footer\n');
  }
  //if url == "/" && POST
    //redirect to /:username

}
//Handle HTTP route GET /:username i.e./chalkers
function user(request, response){
  // if url === "/..."
  //user is /username, remove first slash to get username
  var username = request.url.replace("/", "");
  if (username.length > 0) {
    response.writeHead(200, {'Content-Type':'text/plain'});
    response.write('Header\n');

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

      //store values we need
      var values = {
        avatarUrl: profileJSON.gravatar_url, //root of JSON object
        username: profileJSON.profile_name,
        badges: profileJSON.badges.length,
        javascriptPoints: profileJSON.points.JavaScript
      }

      //simple response
      response.write(values.username + ' has ' + values.badges + ' badges\n');
      response.end('Footer\n');

    });

    // on "error"
    studentProfile.on("error", function(error){
      //show error
      //response.write(error.message + '\n');
      //response.end('Footer\n');
    });

  }
}

module.exports.home = home;
module.exports.user = user;
Steve Anderson
Steve Anderson
4,850 Points

This is the app.js

var router = require('./router.js');

//Use node to perform profile look ups and server our template via HTTP. Look at user's bade count and JavaScript points from a web browser.

//Create a seb server

const http = require('http');

const hostname = '';
const port = 3000;

const server = http.createServer((request, response) => {
  router.home(request, response);
  router.user(request, response);
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});


//Function that handles the reading of files and merge in values.
  //read from file and get a string
    //merge values into String

4 Answers

Brendan Albert
Brendan Albert
32,954 Points

Hello, hello.

So your app.js file looks good to go, everything appears to be in order.

With regards to router.js, here are some things you might try.

1) Do you indeed have the profile.js file from the teacher's notes included in the same directory?

If not, here it is for convenience. If you do have it, does it look like the following?

var EventEmitter = require('events').EventEmitter;
var https        = require('https');
var http         = require('http');
var util         = require('util');

function Profile(username) {

  EventEmitter.call(this);

  profileEmitter = this;

  var request = https.get(`https://teamtreehouse.com/${username}.json`, response => {

    var body = '';

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

    response.on('data', chunk => {
      body += chunk;
      profileEmitter.emit('data', chunk);
    });

    response.on('end', () => {
      if(response.statusCode === 200) {
        try {
          var profile = JSON.parse(body);
          profileEmitter.emit('end', profile);
        } catch (error) {
          profileEmitter.emit('error', error);
        }
      }
    }).on('error', error => {
      profileEmitter.emit('error', error);
    });
  });
}

util.inherits(Profile, EventEmitter);

module.exports = Profile;

I ask because the error message ERR_INCOMPLETE_CHUNK sounds like it could be coming from something in this file.

2) Does the error change if you uncomment the code inside the studentProfile.on('error') block, inside router.js?

3) I'm not as familiar with Workspaces but is it possible the Workspace you are using is listening on a port other than 3000?

Brendan Albert
Brendan Albert
32,954 Points

Actually, in fact the problem might be in app.js

Change

const hostname = '';

to

const hostname = '127.0.0.1';

That might be it.

Steve Anderson
Steve Anderson
4,850 Points

If I uncomment the code inside the studentProfile.on('error') block, inside router.js I get:

Header
There was an error getting the profile for chaulkers. (Not Found)
Footer

I noticed this earlier when I went forward in the course to see if I could get other code to work. I tried changing my router file to match yours, and I get the same error. I tried substituting '127.0.0.1' for hostname, and I got a message that said:

Workspace Unavailable This is a preview link for a Treehouse Workspace that is not currently active.

Brendan Albert
Brendan Albert
32,954 Points

I realized after I sent that comment that 'localhost' only applies if you are running the server on your local machine, versus using TreeHouse's WorkSpaces server.

I would be happy to walk you through setting Node up on your local machine if you want to get your hands dirty :D It really is not bad, and it could be that Workspaces is wonky right now.

Nicholas Pretorius
Nicholas Pretorius
18,684 Points

Hi,

The error will be thrown because the username being referenced above is "chaulkers" which does not exist.

Username should be "chalkers".

Steve Anderson
Steve Anderson
4,850 Points

Hey Brendan, thanks for your help. I ended up running node on my local machine and now the program works. Must have been a Workspaces thing.

Brendan Albert
Brendan Albert
32,954 Points

Awesome, I am glad you figured it out!