我已经做了一个表单。成功提交表单后,我想将页面重定向到thankyou页面(另一个ejs)视图。以某种方式,如果我使用res.redirect()不能正常工作,但是使用res.send('谢谢')则可以正常工作。
以下是我的app.js的代码:
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');
var routes = require('./routes/index');
var users = require('./routes/users');
var thankyou = require('./routes/thankyou');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
var engine = require('ejs-locals');
app.engine('ejs', engine);
app.set('view engine', 'ejs');
// 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')));
mongoose.connect('mongodb://localhost/my-data');
app.use('/', routes);
app.use('/users', users);
app.use('/thankyou', thankyou);
var Schema = new mongoose.Schema({
name : String,
email : String
});
var user = mongoose.model('Users', Schema);
app.post('/test',function(req,res){
new user({
email : req.body.email,
name : req.body.name
}).save(function(err, doc){
if(err){
console.log('boo');
}
else{
console.log('innner');
res.redirect("/thankyou");
res.end();
}
})
})
谢谢路线:
var express = require('express');
var router = express.Router();
router.get('/thankyou', function(req, res) {
res.render('thankyou');
});
module.exports = router;
谢谢你的看法
<h2>THANKS/h2>
最佳答案
这个:
app.use('/thankyou', thankyou);
...与此结合:
router.get('/thankyou', ...);
...创建与
/thankyou/thankyou
匹配的路线。相反,您可能想要这样:
router.get('/', ...);
关于javascript - 为什么我的ejs无法在重定向时呈现?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34289146/