猿问

为什么express无法在views文件夹中查找ejs文件?

开始学习NodeJS并Express。


// 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


慕的地10843
浏览 61回答 1
1回答

胡说叔叔

我得到了它。有人建议添加。app.set('view engine', 'ejs')这本身不起作用,但后来我环顾四周,发现我还需要添加以下行。app.set('views', path.join(__dirname, 'views'));我遵循的教程没有任何此类代码,所以我有点困惑。谢谢您的帮助。
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答