我有一个todo应用程序。我在/ todo上从UI发出帖子请求后,我想将我重定向回/ todo并列出所有todo任务。这不会发生,在我提交任务后,它会留下一个空白页面。
// POST /todo
app.post('/todo', function(req, res) {
var body = _.pick(req.body, 'description', 'completed');
if (!_.isBoolean(body.completed) || !_.isString(body.description) || body.description.trim().length === 0) {
return res.status(400).send();
}
new Todo({
description: body.description.trim(),
id: todoNextId,
completed: body.completed
}).save(function(err, todo, count) {
res.redirect('/todo');
});
todoNextId = todoNextId++;
});
// GET /todo
app.get('/todo', function(req, res) {
Todo.find(function(err, todos, count) {
res.render('pages/todo', {
todos: todos
});
});
});
Ejs看起来像这样:
<form class="form-inline" action="/todo" method="post">
<div class="form-group">
<input type="text" class="form-control" name="description" >
</div>
<div class="checkbox">
<input type="checkbox" name="completed"> Completed
</div>
<button type="submit" class="btn btn-default">Add task</button>
</form>
<br>
<p>Current tasks:</p>
<% todos.forEach( function( todo ){ %>
<p><%= todo.description %></p>
<% }); %>