我正在尝试在ubuntu 16.04中使用nodejs,我安装了node和npm但是我遇到了这个错误" TypeError:无法读取属性' _locals'未定义"当我尝试这个时:
var express = require("express");
app = express();
bodyParser = require("body-parser");
mongoose = require("mongoose");
app.set("view engine", "ejs");
app.get("/", function(req,res){
app.render("index");
});
app.listen(3000, function(){
console.log("Server Started!");
})
它在终端输出:
Server Started!
TypeError: Cannot read property '_locals' of undefined
at EventEmitter.render (/home/luis/Documents/work/webdevBootcamp/test/node_modules/express/lib/application.js:548:11)
at /home/luis/Documents/work/webdevBootcamp/test/app.js:9:6
at Layer.handle [as handle_request] (/home/luis/Documents/work/webdevBootcamp/test/node_modules/express/lib/router/layer.js:95:5)
at next (/home/luis/Documents/work/webdevBootcamp/test/node_modules/express/lib/router/route.js:131:13)
at Route.dispatch (/home/luis/Documents/work/webdevBootcamp/test/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/home/luis/Documents/work/webdevBootcamp/test/node_modules/express/lib/router/layer.js:95:5)
at /home/luis/Documents/work/webdevBootcamp/test/node_modules/express/lib/router/index.js:277:22
at Function.process_params (/home/luis/Documents/work/webdevBootcamp/test/node_modules/express/lib/router/index.js:330:12)
at next (/home/luis/Documents/work/webdevBootcamp/test/node_modules/express/lib/router/index.js:271:10)
at expressInit (/home/luis/Documents/work/webdevBootcamp/test/node_modules/express/lib/middleware/init.js:33:5)
at Layer.handle [as handle_request] (/home/luis/Documents/work/webdevBootcamp/test/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/home/luis/Documents/work/webdevBootcamp/test/node_modules/express/lib/router/index.js:312:13)
at /home/luis/Documents/work/webdevBootcamp/test/node_modules/express/lib/router/index.js:280:7
at Function.process_params (/home/luis/Documents/work/webdevBootcamp/test/node_modules/express/lib/router/index.js:330:12)
at next (/home/luis/Documents/work/webdevBootcamp/test/node_modules/express/lib/router/index.js:271:10)
at query (/home/luis/Documents/work/webdevBootcamp/test/node_modules/express/lib/middleware/query.js:44:5)
当我加载localhost:3000时,它不会渲染ejs文件或让我使用send()函数
app.get("/", function(req,res){
app.send("whatever");
});
它说:
TypeError: app.send is not a function
我安装了express和ejs模块(运行npm install i -S express ejs mongoose body-parser)
答案 0 :(得分:3)
问题在于我在做什么
app.get("/", function(req,res){
app.send("whatever");
});
而不是
app.get("/", function(req,res){
res.send("whatever"); // res instead of app
});
答案 1 :(得分:1)
请设置您的views目录。 示例:如果索引模板文件位于'src'
中app.set('views', __dirname + '/src');