我尝试了其他各种线程,但没有一个帮助过。 我正在尝试使用快递应用程序渲染ejs文件。但是在监听端口时会显示以下错误。
错误
Error: Failed to lookup view "login"
at Function.app.render (C:\Users\darsh\Desktop\Semester_1\SSDI\Sprint 3\workspace\node_modules\express\lib\application.js:495:17)
at ServerResponse.res.render (C:\Users\darsh\Desktop\Semester_1\SSDI\Sprint 3\workspace\node_modules\express\lib\response.js:756:7)
at C:\Users\darsh\Desktop\Semester_1\SSDI\Sprint 3\workspace\server.js:150:13
at callbacks (C:\Users\darsh\Desktop\Semester_1\SSDI\Sprint 3\workspace\node_modules\express\lib\router\index.js:161:37)
at param (C:\Users\darsh\Desktop\Semester_1\SSDI\Sprint 3\workspace\node_modules\express\lib\router\index.js:135:11)
at pass (C:\Users\darsh\Desktop\Semester_1\SSDI\Sprint 3\workspace\node_modules\express\lib\router\index.js:142:5)
at Router._dispatch (C:\Users\darsh\Desktop\Semester_1\SSDI\Sprint 3\workspace\node_modules\express\lib\router\index.js:170:5)
at Object.router (C:\Users\darsh\Desktop\Semester_1\SSDI\Sprint 3\workspace\node_modules\express\lib\router\index.js:33:10)
at next (C:\Users\darsh\Desktop\Semester_1\SSDI\Sprint 3\workspace\node_modules\express\node_modules\connect\lib\proto.js:190:15)
at Object.handle (C:\Users\darsh\Desktop\Semester_1\SSDI\Sprint 3\workspace\node_modules\connect-flash\lib\flash.js:21:5)
at next (C:\Users\darsh\Desktop\Semester_1\SSDI\Sprint 3\workspace\node_modules\express\node_modules\connect\lib\proto.js:190:15)
at SessionStrategy.strategy.pass (C:\Users\darsh\Desktop\Semester_1\SSDI\Sprint 3\workspace\node_modules\passport\lib\middleware\authenticate.js:338:9)
at SessionStrategy.authenticate (C:\Users\darsh\Desktop\Semester_1\SSDI\Sprint 3\workspace\node_modules\passport\lib\strategies\session.js:75:10)
at attempt (C:\Users\darsh\Desktop\Semester_1\SSDI\Sprint 3\workspace\node_modules\passport\lib\middleware\authenticate.js:361:16)
at Object.authenticate [as handle] (C:\Users\darsh\Desktop\Semester_1\SSDI\Sprint 3\workspace\node_modules\passport\lib\middleware\authenticate.js:362:7)
at next (C:\Users\darsh\Desktop\Semester_1\SSDI\Sprint 3\workspace\node_modules\express\node_modules\connect\lib\proto.js:190:15)
我的文件结构
- Workspace
- client
- css
- js
- views
- login.ejs
- index.ejs
- server.js
代码的小片段:
var express = require('express');
var app = express();
var server = http.createServer(app);
app.set('views', path.resolve(__dirname, '/views'));
app.set('view engine', 'ejs'); // set up ejs for templating
app.listen(8080);
app.get('/', function(req, res) {
res.render('login.ejs'); // load the login.ejs file
});
app.get('/index', function(req, res) {
res.render('index.ejs')
});
答案 0 :(得分:0)
希望这可以提供帮助。在这里,他们提供了ejs文件的完整路径。 https://www.google.co.in/amp/scotch.io/amp/tutorials/use-ejs-to-template-your-node-application
答案 1 :(得分:0)
使用path.join而不是path.resolve,
因为path.resolve将导致/views
。
尝试使用path.join(__ dirname +'/ views')。 别忘了把const path = require('path')。
答案 2 :(得分:0)
You can use this one.
var express = require('express')
var http = require('http')
var app = express()
var server = http.createServer(app)
app.set('view engine', 'ejs')
app.listen(8080)
app.get('/', (req, res) => {
res.render('login')
})