response.locals提示未定义,求解。
发布于 1 年前 作者 jiangli1990 980 次浏览 来自 问答

文件:messages.js

var express = require(‘express’); var res = express.response;

res.message = function(msg, type){ type = type || ‘info’; var sess = this.req.session; sess.messages = sess.messages || []; sess.messages.push({ type: type, string: msg }); };

res.error = function(msg){ return this.message(msg, ‘error’); };

module.exports = function(req, res, next){

res.locals.messages = req.session.messages || [];
console.log(res.locals.messages);
res.locals.removeMessages = function(){
    req.session.messages = [];
};
next();

}; 上诉代码,当我在app.js中调用接口时,报错未定义,求大神提示,我问题出在哪里 下面是app.js代码: var createError = require(‘http-errors’); var express = require(‘express’); var path = require(‘path’); var cookieParser = require(‘cookie-parser’); var session = require(‘express-session’); var logger = require(‘morgan’); 下面这行里是我调用上面messages代码 var messages = require(’./models/lib/messages’);

var indexRouter = require(’./routes/index’); var usersRouter = require(’./routes/users’); var photosRouter = require(’./routes/photos’); var photosUpdata = require(’./routes/updata’); var photosRegister = require(’./routes/register’); var photosLogin = require(’./routes/login’);

var app = express();

// view engine setup app.set(‘views’, path.join(__dirname, ‘views’)); app.set(‘view engine’, ‘jade’);

app.use(logger(‘dev’)); app.use(express.json()); app.use(express.urlencoded({ extended: false })); app.use(cookieParser()); app.use(session({ secret: ‘12345’, name: ‘testapp’, //这里的name值得是cookie的name,默认cookie的name是:connect.sid cookie: {maxAge: 80000 }, //设置maxAge是80000ms,即80s后session和相应的cookie失效过期 resave: false, saveUninitialized: true,})); app.use(messages()); app.use(express.static(path.join(__dirname, ‘public’)));

app.use(’/’, indexRouter); app.use(’/users’, usersRouter); app.use(’/p’, photosRouter); app.use(’/u’, photosUpdata); app.use(’/’, photosRegister); app.use(’/’, photosLogin);

// catch 404 and forward to error handler app.use(function(req, res, next) { next(createError(404)); });

// 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; 报错内容:/usr/local/bin/node /Users/johnny/WebstormProjects/JBBlog/bin/www /Users/johnny/WebstormProjects/JBBlog/models/lib/messages.js:40 res.locals.messages = req.session.messages || []; ^

TypeError: Cannot read property ‘locals’ of undefined at module.exports (/Users/johnny/WebstormProjects/JBBlog/models/lib/messages.js:40:9) at Object.<anonymous> (/Users/johnny/WebstormProjects/JBBlog/app.js:32:9) at Module._compile (module.js:660:30) at Object.Module._extensions…js (module.js:671:10) at Module.load (module.js:573:32) at tryModuleLoad (module.js:513:12) at Function.Module._load (module.js:505:3) at Module.require (module.js:604:17) at require (internal/module.js:11:18) at Object.<anonymous> (/Users/johnny/WebstormProjects/JBBlog/bin/www:7:11)

Process finished with exit code 1

回到顶部