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

Cannot find server or Server didnt respond error.

Hey gang,

So I followed all the directions on the course but in the end I got Data not received or server not found error. And I even changed the server to be a https server.

This is the app.js:

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

    var server = https.createServer({},function(req,res) {
        router.homeRoute(req, res);
        router.userRoute(req, res);
    });

    server.listen(3000);
    console.log('Listening on port 3000');

This is the router.js;

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

    function homeRoute(req, res) {
        if(req.url === '/') {
            if(req.method.toLowerCase() === 'get') {
                res.writeHead(200, {'Content':'text/html'});
                renderer.view('header', {}, res);
                renderer.view('search', {}, res);
                renderer.view('footer', {}, res);
                res.end();
            } else {
            }
        }
    }

    function userRoute(req, res) {
        var username = req.url.replace('/','');
        if(username.length > 0){
            var userProfile = new Profile(username);
            res.writeHead(200, {'Content':'text/html'});
            console.log('The data I got from the request is ',jsonData);
            renderer.view('header', {}, res);
            userProfile.on('end', function(jsonData) {
                var values = {
                    avatarUrl: jsonData.gravatar_url,
                    username: jsonData.profile_name,
                    badges: jsonData.badges.length,
                    javascriptPoints: jsonData.points.JavaScript
                };
                renderer.view('profile', values, res);
                renderer.view('search', {}, res);
                renderer.view('footer', {}, res);
                res.end();
            });

            userProfile.on('error', function(error) {
                res.writeHead(200, {'Content':'text/html'});
                console.log('The data I got from the request is ',{errorMessage: error.message});
                renderer.view('header', {}, res);
                renderer.view('error', {errorMessage: error.message}, res);
                renderer.view('search', {}, res);
                renderer.view('footer', {}, res);
                res.end();
            });
        }
    }

    module.exports.homeRoute = homeRoute;
    module.exports.userRoute = userRoute;

I didn't update it to redirect the post request to a get request but even when I make a GET request to '/' route, I still get an error.

Help?

1 Answer

Since Treehouse updated to using https, the code gets a bit wonky to fix. You actually don't need to include https in your app.js file. Instead, I believe you need both http and https in your profile.js file.

app.js

// This is my app.js file. You shouldn't need https here.
var http = require('http');
var router = require('./router.js');

http.createServer(function (request, response) {
  router.home(request, response);
  router.user(request, response);
}).listen(1337, '127.0.0.1');

console.log('Server running at http://127.0.0.1:1337/');

profile.js

// This is not a complete file. I've only included the relevant portions.
var https = require("https");
var http = require("http");

function Profile(username) {
  // . . .
  // You'll need to use https here to get the Treehouse information.
  var request = https.get("https://teamtreehouse.com/" + username + ".json", function(response) {
    // . . .
    if (response.statusCode !== 200) {
      request.abort();
      // You'll need to use http here to get the status code.
      profileEmitter.emit("error", new Error("There was an error getting the profile for " + username + ". (" + http.STATUS_CODES[response.statusCode] + ")"));
    };
    // . . .
  });
};
// . . .

Hopefully that makes sense, but please let me know if you have any questions. It can be a bit tricky following along, now that the videos don't necessarily match the current protocol. Happy coding! :)

Yes you are right. Thank you very much!