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

Error: ENOENT: no such file or directory

Hello,

Im trying to run my code but im getting this error on node. var fs = require('fs');

function mergeValues(values, content) { //Cycle over the keys //Replace all {{key}} with the value from the values object. for(var key in values) { content = content.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"}).toString();
fileContents = mergeValues(values, fileContents);
response.write(fileContents);

};

module.exports.view = view;

I dont understand why its not finding my files in the views folder. Can someone please help me ?

2 Answers

someone had the exact same problem as me some time ago and created a github with the code : https://github.com/jg-digital-media/node-site . maybe it will be easier to read the code in here?

Why did you use that .toString() for?