无法查找视图节点JavaScript

时间:2017-10-10 07:34:12

标签: javascript angularjs node.js express ejs

当我尝试在浏览器中呈现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)

项目结构 上面的代码项目结构图:
The above code Project structure image

1 个答案:

答案 0 :(得分:2)

将视图文件index.js重命名为index.ejs