Expressjs POST方法后重定向不正确
Expressjs Incorrect Redirect after POST method
我有一个表单页面,它被路由到/admin/posts/create。提交表单时,应该有一个重定向到"/"的POST方法。问题是,当我提交表单时,重定向不允许帖子通过。CCD_ 1。是什么阻止了这种情况的发生?
routes.js:
var express = require('express');
var router = express.Router();
var blogDB = require('../config/blogDB.js');
var Blogpost = require('./models/blogModel.js');
var paginate = require('express-paginate');
//index
router.use(paginate.middleware(10, 50));
router.route('/')
// START GET method
.get(function(req, res, next) {
Blogpost.paginate({}, req.query.page, req.query.limit, function(err, pageCount, blogpost, itemCount) {
if (err) return next(err)
if (err)
res.send(err);
blogpost.title = req.body.title; // get the blog title
blogpost.author = req.body.author; // get the author name
blogpost.tagline = req.body.tagline; // get tagline
blogpost.content = req.body.content; // get the blog content
blogpost.category = req.body.category; // get the category
blogpost.tags = req.body.tags; // get the tags
res.format({
html: function() {
res.render('pages/index', {
blogpost: blogpost,
pageCount: pageCount,
itemCount: itemCount
})
},
json: function() {
res.json({
object: 'blogpost',
has_more: paginate.hasNextPages(req)(pageCount),
data: blogpost
})
}
}); // END res.format(html, json)
}); // END Blogpost.paginate
}); // END GET method
router.route('/admin/posts/create')
// START POST method
.post(function(req, res) {
var blogpost = new Blogpost(); // create a new instance of a Blogpost model
blogpost.title = req.body.title; // set the blog title
blogpost.author = req.body.author; // set the author name
blogpost.tagline = req.body.tagline; // set the tagline
blogpost.content = req.body.content; // set the blog content
blogpost.category = req.body.category; // set the category
blogpost.tags = req.body.tags; // set the tags
//Save Blog Post
blogpost.save(function(err) {
if (err)
res.send(err);
res.redirect(303, '/'); //NEEDS TO BE CHANGED
});
}) // END POST method
.get(function(req, res) {
res.render('pages/blogpost-create');
});
blogpost-create.ejs:
<html>
<head>
<% include ../partials/head %>
<script src="//cdn.ckeditor.com/4.4.5/standard/ckeditor.js"></script>
</head>
<body>
<header>
<% include ../partials/header %>
</header>
<div class="grid grid-pad">
<div class="col-1-1">
<h1>Blog Create</h1>
<form action="/" method="POST">
Title: <input type="text" name="title"><br>
Author:
<select>
<option>Author</option>
</select><br>
Category:
<select>
<option>Analytics/SEO/SEM</option>
<option>Advice</option>
<option>Programming</option>
<option>Thoughts</option>
</select><br>
Tagline: <input type="text" maxlength:"160" name="tagline"><br>
Content:<br>
<textarea name="blog-editor" id="blog-editor" rows="10" cols="80">
Text editor.
</textarea><br>
Tags: <input type="text" name="tags"><br>
<input type="submit" value="Submit">
</form>
</div>
</div>
<script>
// Replace the <textarea id="blog-editor"> with a CKEditor
// instance, using default configuration.
CKEDITOR.replace( 'blog-editor' );
</script>
<footer>
<% include ../partials/footer %>
</footer>
</body>
</html>
没有路由POST /
,只有路由GET /
。
添加
.post(function(req, res, next) {
res.json({test: 'Hello World!'});
})
在router.route('/')
之后重试。
相关文章:
- 在phonegap应用程序内部重定向不起作用
- 通过链接重定向不;我不在jstree中工作
- 角度 JS + 重定向不起作用
- 重定向不带哈希的URL-Ember.JS
- 从帖子返回后,Javascript重定向不起作用
- 倒计时和重定向的正确方法
- 重定向不起作用
- 登录后PHP登录脚本重定向不起作用
- 使用self.location.href重定向不会'不起作用
- 按tab键不会重定向到正确的文本框
- 带有SetTimout的Javascript重定向不工作
- 什么'这是重定向不受支持的浏览器的最简单方法
- JavaScript窗口重定向不会启动
- 按钮单击不重定向到正确的 URL
- Response.重定向不能在会话超时的页面回调中调用
- Javascript重定向不会到正确的URL
- 使用window.location.replace重定向不起作用
- Expressjs POST方法后重定向不正确
- 不能用angular.js的ui-router重定向到正确的路径
- Modal外部Url重定向不正确