初学NODE,在学习MYBOLG的例子时,使用req.flash('error', '!')时,报错 Failed to lookup view 'error'
发布于 7个月前 作者 lixiaodog 1032 次浏览

//代码如下 router.post('/reg’, function(req, res) { var name = req.query.name, password = req.body.password, password_re = req.body[‘password-repeat’];

//检验用户两次输入的密码是否一致
if (password_re != password) {
    req.flash('error', '两次输入的密码不一致!');
    return res.redirect('/reg');//返回主册页
}

错误日志: Error: Failed to lookup view “error” in views directory “D:\WORK\node\mytest\vie ws” at Function.app.render (D:\WORK\node\mytest\node_modules\express\lib\applica tion.js:492:17) at ServerResponse.res.render (D:\WORK\node\mytest\node_modules\express\lib\r esponse.js:802:7) at Layer.module.exports [as handle] (D:\WORK\node\mytest\app.js:67:9) at trim_prefix (D:\WORK\node\mytest\node_modules\express\lib\router\index.js :235:17) at D:\WORK\node\mytest\node_modules\express\lib\router\index.js:208:9 at Function.proto.process_params (D:\WORK\node\mytest\node_modules\express\l ib\router\index.js:269:12) at IncomingMessage.next (D:\WORK\node\mytest\node_modules\express\lib\router \index.js:199:19) at fn (D:\WORK\node\mytest\node_modules\express\lib\response.js:797:25) at Function.app.render (D:\WORK\node\mytest\node_modules\express\lib\applica tion.js:494:14) at ServerResponse.res.render (D:\WORK\node\mytest\node_modules\express\lib\r esponse.js:802:7)

app.js如下 var fs = require(‘fs’); var accessLog = fs.createWriteStream('access.log’, {flags: 'a’}); var errorLog = fs.createWriteStream('error.log’, {flags: 'a’});

var express = require(‘express’); var path = require(‘path’); var favicon = require(‘static-favicon’); var logger = require(‘morgan’); var cookieParser = require(‘cookie-parser’); var bodyParser = require(‘body-parser’);

var routes = require(‘./routes/index’);

var session = require(‘express-session’);

var MongoStore = require(‘connect-mongo’)(session); var settings = require(‘./settings’);

var flash = require(‘connect-flash’); var app = express();

// view engine setup app.set('views’, path.join(__dirname, ‘views’)); app.set('view engine’, ‘ejs’); app.use(flash());

app.use(favicon()); app.use(logger(‘dev’)); app.use(bodyParser.json()); app.use(bodyParser.urlencoded()); app.use(cookieParser()); app.use(express.static(path.join(__dirname, ‘public’)));

//app.use('/’, routes);

routes(app);

app.use(session({ secret:settings.cookieSecret, key:settings.db, saveUninitialized: true, resave: true, cookie:{maxAge:100060602430}, store:new MongoStore({db:settings.db}) })); /// 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 handlers

// development error handler // will print stacktrace if (app.get(‘env’) === ‘development’) { app.use(function(err, req, res, next) { res.status(err.status || 500); res.render('error’, { message: err.message, error: err }); }); }

// production error handler // no stacktraces leaked to user app.use(function(err, req, res, next) { res.status(err.status || 500); res.render('error’, { message: err.message, error: {} }); });

module.exports = app;

10 回复

有哪位前辈知道的,麻烦告诉下

有人知道为什么吗?来个热心人告诉我下吧

教程太旧了。

你可以把你的错误日志发出来。

看你的描述视乎是说找不到 error 这张页面。

的确像你说的那样,是找不到ERROR这个VIEW,但FLASH这个模块根本不需要用到这个VIEW啊,错误就发生在req.flash()这句 错误日志: Error: Failed to lookup view “error” in views directory “D:\WORK\node\mytest\vie ws” at Function.app.render (D:\WORK\node\mytest\node_modules\express\lib\applica tion.js:492:17) at ServerResponse.res.render (D:\WORK\node\mytest\node_modules\express\lib\r esponse.js:802:7) at Layer.module.exports [as handle] (D:\WORK\node\mytest\app.js:67:9) at trim_prefix (D:\WORK\node\mytest\node_modules\express\lib\router\index.js :235:17) at D:\WORK\node\mytest\node_modules\express\lib\router\index.js:208:9 at Function.proto.process_params (D:\WORK\node\mytest\node_modules\express\l ib\router\index.js:269:12) at IncomingMessage.next (D:\WORK\node\mytest\node_modules\express\lib\router \index.js:199:19) at fn (D:\WORK\node\mytest\node_modules\express\lib\response.js:797:25) at Function.app.render (D:\WORK\node\mytest\node_modules\express\lib\applica tion.js:494:14) at ServerResponse.res.render (D:\WORK\node\mytest\node_modules\express\lib\r esponse.js:802:7)

看了新教程,使用EXPRESS最新版,还是报这个错误

myblog这个例子,如此调用,是不会报这个错误的,但我用的是EXPRESS4.X,我怀疑是我哪个地方的配置有问题

当然有问题了,这个例子是3.0版本的,可以这样使用: app.use(function(req, res, next){

    var error=req.flash('error');
    
    var success=req.flash('success');
    res.locals.user = req.session.user;
    console.log("user");
    res.locals.error = error.length?error:null;
    
    res.locals.success = success.length?success:null;
    next();
});

我也出现过这种情况。 你app.js里面配置的app.set('view engine’, ‘html’); 或者 app.set('view engine’, ‘ejs’); 与views/error的后缀一样吗

回到顶部