当我尝试在浏览器中呈现URL以显示 index.js 时,会发生以下错误:
**Error:** Failed to lookup view "index" in views directory "D:\NodeJs\Example\5expressnodjsexmp\views"
以下是我的应用程序代码
//Headers
var express = require('express');
var bodyParser = require('body-parser');
var path = require('path');
var app = express();
//body parser
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
// set static path
app.use(express.static(path.join(__dirname,'public')));
// EJS View engine
app.set('view engine','ejs');
app.set('views',path.join(__dirname,'views'));
app.get('/',function(req,res){
res.render('index');
});
app.listen(3000, function(){
console.log('Server Started at 3000.....');
});
错误抛出:
Error: Failed to lookup view "index" in views directory "D:\NodeJs\Example\5expressnodjsexmp\views"
at EventEmitter.render (D:\NodeJs\Example\5expressnodjsexmp\node_modules\express\lib\application.js:580:17)
at ServerResponse.render (D:\NodeJs\Example\5expressnodjsexmp\node_modules\express\lib\response.js:1008:7)
at D:\NodeJs\Example\5expressnodjsexmp\app.js:31:6
at Layer.handle [as handle_request] (D:\NodeJs\Example\5expressnodjsexmp\node_modules\express\lib\router\layer.js:95:5)
at next (D:\NodeJs\Example\5expressnodjsexmp\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (D:\NodeJs\Example\5expressnodjsexmp\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (D:\NodeJs\Example\5expressnodjsexmp\node_modules\express\lib\router\layer.js:95:5)
at D:\NodeJs\Example\5expressnodjsexmp\node_modules\express\lib\router\index.js:281:22
at Function.process_params (D:\NodeJs\Example\5expressnodjsexmp\node_modules\express\lib\router\index.js:335:12)
at next (D:\NodeJs\Example\5expressnodjsexmp\node_modules\express\lib\router\index.js:275:10)
答案 0 :(得分:2)
将视图文件index.js
重命名为index.ejs