简体   繁体   English

错误:无法查找视图“索引”

[英]Error: Failed to lookup view “index”

I tried following various other threads but none of them helped. 我尝试遵循其他各种线程,但没有一个有帮助。 I am trying to render ejs files, using express app. 我正在尝试使用Express应用程序渲染ejs文件。 But on listening on the port it show the following error. 但是在侦听端口时会显示以下错误。

Error 错误

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)

My File Structure 我的档案结构

- Workspace
   - client 
      - css
      - js
   - views
      - login.ejs
      - index.ejs
  - server.js

Small snippet of Code: 一小段代码:

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')
    });

Hope this can help. 希望这会有所帮助。 Here, they are giving the complete path to the ejs file. 在这里,他们提供了ejs文件的完整路径。 https://www.google.co.in/amp/scotch.io/amp/tutorials/use-ejs-to-template-your-node-application https://www.google.co.in/amp/scotch.io/amp/tutorials/use-ejs-to-template-your-node-application

Use path.join instead of path.resolve, Because path.resolve will result in /views . 使用path.join而不是path.resolve,因为path.resolve将导致/views

Try using path.join(__dirname + '/views'). 尝试使用path.join(__ dirname +'/ views')。 Do not forget to put const path = require('path'). 不要忘了把const path = require('path')放进去。

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')
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM