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

Amy Kang
Amy Kang
17,188 Points

No Method Error for "view"

I'm getting this error:

/home/amy/javascript/node2/router.js:10 response.view("header", {}, response); ^ TypeError: Object #<ServerResponse> has no method 'view'

renderer.js

var fs = require("fs");

function view(templateName, values, response) {
  //Read from the template files
  var fileContents = fs.readFileSync('./views/' + templateName + '.html');
  //Insert values in to the content
  //Write out the contents to the response
  response.write(fileContents);

}

module.exports.view = view;

router.js

var Profile = require("./profile.js");
var renderer = require("./renderer.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.view("header", {}, response);
  response.write("Search\n");
  response.end("Footer\n");

  }
}

1 Answer

Amy Kang
Amy Kang
17,188 Points

It's supposed to be renderer.view not response.view...doh!