Nodejs:无法在视图目录

时间:2017-07-16 00:15:50

标签: angularjs node.js express localhost

我目前正在开发一个在nodejs服务器上运行express的角度网站。当我开始在localhost上运行时它通常会完全加载,但就在昨天它突然开始抛出这个错误。

GET / 500 3.467 ms - 1595 错误:无法在视图目录“C:\ node \ nodetest1 \ routes \ views”中查找视图“错误”     在EventEmitter.render(C:\ node \ nodetest1 \ node_modules \ express \ lib \ application.js:579:17)     在ServerResponse.render(C:\ node \ nodetest1 \ node_modules \ express \ lib \ response.js:960:7)     在C:\ node \ nodetest1 \ server.js:63:7     在Layer.handle_error(C:\ node \ nodetest1 \ node_modules \ express \ lib \ router \ layer.js:71:5)     在trim_prefix(C:\ node \ nodetest1 \ node_modules \ express \ lib \ router \ index.js:310:13)     在C:\ node \ nodetest1 \ node_modules \ express \ lib \ router \ index.js:280:7     在Function.process_params(C:\ node \ nodetest1 \ node_modules \ express \ lib \ router \ index.js:330:12)     在下一个(C:\ node \ nodetest1 \ node_modules \ express \ lib \ router \ index.js:271:10)     在Layer.handle_error(C:\ node \ nodetest1 \ node_modules \ express \ lib \ router \ layer.js:67:12)     在trim_prefix(C:\ node \ nodetest1 \ node_modules \ express \ lib \ router \ index.js:310:13)     在C:\ node \ nodetest1 \ node_modules \ express \ lib \ router \ index.js:280:7     在Function.process_params(C:\ node \ nodetest1 \ node_modules \ express \ lib \ router \ index.js:330:12)     在Immediate.next(C:\ node \ nodetest1 \ node_modules \ express \ lib \ router \ index.js:271:10)     在立即。 (C:\节点\ nodetest1 \ node_modules \表达\ lib中\路由器\ index.js:618:15)     在runCallback(timers.js:674:20)     在tryOnImmediate(timers.js:645:5)

这是我的网站目录树:
nodetest1
   箱子
   路线
      库
      谐音
      意见
   server.js
   的package.json

我已经坚持了一天,并且真的很感激一些帮助。谢谢!

编辑:下面是我的服务器代码。

    var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var mongoose = require('mongoose');

//This connects the Mongo DB
//var mongo = require('mongodb');
//var monk = require('monk');
//var db = monk('localhost:27017/appointment');

mongoose.connect('mongodb://localhost:27017/appointment');



var app = express();

var index = require('./routes/index');
var users = require('./routes/users');

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.engine('ejs', require('ejs').renderFile);
app.set('view engine', 'ejs');


app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'routes')));

//Actually uses the mongodb
//app.use(function(req,res,next){
    //req.db = db;
    //next();
//});




app.use('/', index);
app.use('/users', users);
app.use('/api', require('./routes/api/appointment/'));


// catch 404 and forward to error handler
app.use(function(req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;
  next(err);
});

// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;

2 个答案:

答案 0 :(得分:0)

问题:

res.render('error');

您在视图目录

中没有文件error.ejs

答案 1 :(得分:0)

在您的目录结构中,

views位于routes目录内。

您必须将其放在routes目录或

之外

定义这样的视图路径。

app.set('views', path.join(__dirname, 'routes/views'));