我正在学习 Node.js。为了学习,我正在构建一个基本的应用程序。此时,我正在尝试做一个主/详细信息页面。这是母版页。
/views/master.ejs
<html>
<body>
[items would go here]
<a href="/new">add new</a>
</body>
</html>
用户访问“/”时将看到此页面。当他们点击“添加新”链接时,我会将他们带到“/new”。 View 的代码在这里:
/views/detail.ejs
<html>
<body>
<form id="myForm" method="post" action="/new">
<button type="submit">Submit</button>
</form>
</body>
</html>
我已经成功地工作了。但是,当用户单击“提交”时,我不确定如何将它们重定向回“/” View 。这时候我的路由代码是这样的:
var router = require('express').Router();
/* GET the master page. */
router.get('/', function(req, res) {
res.render('master', { });
});
/* GET the new page */
router.get('/new', function(req, res) {
res.render('detail', { });
});
/* POST new item */
router.post('/new', function(req, res) {
console.log('creating item...');
// The following line just shows "undefined" in the browser"
// I don't know if 1) Its correct or 2) If I should do it some other way
res.redirect(200, '/', { });
});
如何在主从页面场景中处理加载 View ?
谢谢!
最佳答案
您是否尝试过在不传递 View 模型的情况下进行重定向?例如:
res.redirect('/projects/setup/website/details');
documentation 中没有任何样本显示正在传递的 View 模型。
关于javascript - Node.js - 使用主页面/详细信息页面在 Express 中重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32490937/