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

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,253 Points

Error: ENOENT: no such file or directory... when requesting URL.

Well, I gave it a good go but I suspect there's some sort of breaking change in the code as it;s been quite a while since the course was released. From what I gather I need to find a way to get the string representation of the URL for the templates but all I keep getting back is an object. [object Object]

I've tried stringify and .toString but I only get the same error back. Any more ideas? Thanks

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

function home(request, response) {
    if(request.url === "/") {
        response.writeHead(200, {"content-type":"text/plain"});
        Render.view("header", {}, response);
        Render.view("search", {}, response);
        Render.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"});
        Render.view("header", {}, response);

        //get json from Treehouse API
        var studentProfile = new Profile(username);

        studentProfile.on("end", function(profileJSON){
            //show profile

            //store the values which we need

            var values = {
                avatarUrl: stringify(profileJSON.gravatar_url),
                username: stringify(profileJSON.profile_name),
                badges: stringify(profileJSON.badges.length),
                javascriptPoints: stringify(profileJSON.points.JavaScript),
            }

            //simple response
            response.write("The user " + values.username + " has " + values.badges + " Treehouse badges!" + "\n");
            Render.view("profile", {values}, response);
            Render.view("footer", {}, response );
            response.end();



        });        

        studentProfile.on("error", function(error){
            //on error
            Render.view({errorMessage: error.message} + "\n");
            Render.view("footer", {}, response);
            response.end();
        });

    }

}

module.exports.home = home;
module.exports.user = user;
render.js
var fs = require("fs");

function mergeValues(values, content) {
    //Cycle over the keys
      //Replace all {{key}} with the value from the values object.
    for(let key in values) {
        content = content.toString().replace("{{" + key + "}}", values[key]);
    }
    //return merge content
    return content;
}

function view(templateName, values, response) {
    //read from the template file


    var fileContents = fs.readFileSync("./views/" + templateName + ".html", {encoding: "utf8"});
    fileContents = mergeValues(values, fileContents);
    response.write(fileContents);
};

module.exports.view = view;
//How to read from files in node. 
1
$ node app
Server running at http://127.0.0.1:3000/
fs.js:646
  return binding.open(pathModule._makeLong(path), stringToFlags(flags), mode);
                 ^

Error: ENOENT: no such file or directory, open 'C:\xampp\htdocs\jgdm-100daysofcode\nodejs\dynamic_site\views\[object Object]        
.html'
    at Object.fs.openSync (fs.js:646:18)
    at Object.fs.readFileSync (fs.js:551:33)
    at Object.view (C:\xampp\htdocs\jgdm-100daysofcode\nodejs\dynamic_site\render.js:17:27)
    at Profile.<anonymous> (C:\xampp\htdocs\jgdm-100daysofcode\nodejs\dynamic_site\router.js:48:20)
    at emitOne (events.js:116:13)
    at Profile.emit (events.js:211:7)
    at ClientRequest.<anonymous> (C:\xampp\htdocs\jgdm-100daysofcode\nodejs\dynamic_site\profile.js:24:28)
    at Object.onceWrapper (events.js:315:30)
    at emitOne (events.js:116:13)
    at ClientRequest.emit (events.js:211:7)

Jonnie@LAPTOP-13ASI1QJ MINGW64 /c/xampp/htdocs/jgdm-100daysofcode/nodejs/dynamic_site (master)
$ node app
Server running at http://127.0.0.1:3000/
fs.js:646
  return binding.open(pathModule._makeLong(path), stringToFlags(flags), mode);
                 ^

Error: ENOENT: no such file or directory, open 'C:\xampp\htdocs\jgdm-100daysofcode\nodejs\dynamic_site\views\[object Object]        
.html'
    at Object.fs.openSync (fs.js:646:18)
    at Object.fs.readFileSync (fs.js:551:33)
    at Object.view (C:\xampp\htdocs\jgdm-100daysofcode\nodejs\dynamic_site\render.js:17:27)
    at Profile.<anonymous> (C:\xampp\htdocs\jgdm-100daysofcode\nodejs\dynamic_site\router.js:48:20)
    at emitOne (events.js:116:13)
    at Profile.emit (events.js:211:7)
    at ClientRequest.<anonymous> (C:\xampp\htdocs\jgdm-100daysofcode\nodejs\dynamic_site\profile.js:24:28)
    at Object.onceWrapper (events.js:315:30)
    at emitOne (events.js:116:13)
    at ClientRequest.emit (events.js:211:7)

1 Answer

I have the same problem, did you vi any chance figue out what was the problem?

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,253 Points

Hi there! I'm sorry, I haven't been able to. I had a quick dip back in and tried to edit for the first string in the directory for the readFilesync() method. It also doesn't like something about the header.html but I don't know what at the moment!