为什么express无法在视图文件夹中查找ejs文件



开始学习NodeJSExpress

// express framework 
var express  = require("express");
var app = express();
app.get("/", (req,res) =>
{
res.render("home.ejs");
})
//port listening 
app.listen(3000, ()=>
[
console.log("Server listening at 3000")
])

视图文件夹位于代码的同一文件夹中,并且具有文件home.ejs

我收到以下错误

Error: Failed to lookup view "home.ejs" in views directory "/Users/username/Desktop/Programming/JavaScript/views"
at Function.render (/Users/username/node_modules/express/lib/application.js:580:17)
at ServerResponse.render (/Users/username/node_modules/express/lib/response.js:1012:7)
at /Users/username/Desktop/Programming/JavaScript/practice/app.js:8:9
at Layer.handle [as handle_request] (/Users/username/node_modules/express/lib/router/layer.js:95:5)
at next (/Users/username/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/Users/username/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/Users/username/node_modules/express/lib/router/layer.js:95:5)
at /Users/username/node_modules/express/lib/router/index.js:281:22
at Function.process_params (/Users/username/node_modules/express/lib/router/index.js:335:12)
at next (/Users/username/node_modules/express/lib/router/index.js:275:10)

我得到了。有人建议添加。

app.set('view engine', 'ejs')

这本身并不奏效,但后来我环顾四周,发现我还需要添加以下行。

app.set('views', path.join(__dirname, 'views'));

我正在学习的教程没有这些代码,所以我有点困惑。

谢谢你的帮助。

相关内容

  • 没有找到相关文章

最新更新