express.js:显示 toastr 消息
Posted
技术标签:
【中文标题】express.js:显示 toastr 消息【英文标题】:express.js: show toastr message 【发布时间】:2016-06-22 14:01:41 【问题描述】:我正在尝试让 Toastr 库在我的 ExpressJS 应用程序中工作!我用 yeoman 'standard' Express Generator 搭建了这个应用程序......
我需要库 express-toastr
并执行以下操作:
在app.js
:
const cookieParser = require('cookie-parser');
const session = require('express-session');
const flash = require('connect-flash');
const toastr = require('express-toastr');
app.use(cookieParser());
app.use(session( secret: 'xxx', saveUninitialized: true, resave: true ));
app.use(flash());
app.use(toastr());
在index.js
const express = require('express');
const router = express.Router();
const httpntlm = require('httpntlm');
router.post('/', function (req, res, next)
// parse inputs
let user = req.body.user || "";
let password = req.body.password || "";
// save in session
req.session.user = user: user, password: password;
// appropriate response to login attempt
if (!req.session.user)
res.status(401).send();
else
req.toastr.success('Successfully logged in.', "You're in!");
res.render('groups',
req: req
);
);
module.exports = router;
在index.jade
#req.toastr.render()
我正在我的<head>
部分中加载这些文件:
link(rel='stylesheet', href='//cdnjs.cloudflare.com/ajax/libs/toastr.js/2.0.2/css/toastr.min.css')
script(src='/components/jquery/dist/jquery.min.js')
script(src='//cdnjs.cloudflare.com/ajax/libs/toastr.js/2.0.2/js/toastr.min.js')
什么都没有显示。我错过了什么???
--更新! --
这是我完整的 app.js 文件。我现在尝试使用 express-flash 并为显示 flash 消息制作专用路由。还是行不通。请帮忙!
'use strict';
const express = require('express');
const path = require('path');
const favicon = require('serve-favicon');
const logger = require('morgan');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const expressSanitizer = require('express-sanitizer');
const login = require('./routes/login');
const apply = require('./routes/apply');
const admin = require('./routes/admin');
var session = require('express-session');
var flash = require('express-flash');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded(extended: false));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(session(
cookie: maxAge: 60000 ,
store: new session.MemoryStore,
saveUninitialized: true,
resave: 'true',
secret: 'secret'
));
app.use(flash());
// Route that creates a flash message using the express-flash module
app.all('/express-flash', function( req, res )
req.flash('success', 'This is a flash message using the express-flash module.');
res.redirect(301, '/');
);
// sanitize inputs
app.use(expressSanitizer());
app.use('/', apply);
app.use('/apply', apply);
app.use('/login', login);
app.use('/admin', admin);
// 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;
【问题讨论】:
【参考方案1】:尝试在你的 app.js 中使用这个中间件,我发现了这个here
app.use(function (req, res, next)
res.locals.toasts = req.toastr.render()
next()
);
然后在你的视图中访问locals如下:
#toasts
这对我有用。
【讨论】:
【参考方案2】:所以我不熟悉您在 index.jade 文件中的语法(!=)。它有什么作用?如果您将索引中的那一行更改为 #req.toastr.render() 它应该可以工作。
【讨论】:
我试过你的建议,但还是不行。你能让它工作吗?语法 != 用于转义代码。我误解了在哪里使用它! jade-lang.com/reference/code以上是关于express.js:显示 toastr 消息的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Django 中使用 toastr 获取成功或失败消息