Nodejs TypeError:undefined不是函数

时间:2015-06-20 09:53:13

标签: javascript node.js

我正在关注Treehouse Nodejs教程并遇到此错误。路由器中的服务器响应无法在渲染器中找到视图方法。

router.js:10
response.view("header", {}, response);
       ^
TypeError: undefined is not a function

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 个答案:

答案 0 :(得分:1)

您未在renderer中的任何位置使用router.js

我认为您打算拨打renderer.view而不是response.view