各种HTTP 3XX重定向代码之间的差异对我来说并不清楚.是的,我已经阅读了规范,但这里的标准和实际做法之间似乎存在一些差异.
该301
重定向代码似乎很清楚:这意味着资源已被永久移动到另一个URI,以及将来的请求应使用URI.
并且307
重定向代码似乎也很清楚:这意味着重定向是临时的,未来的请求仍然应该使用原始URI.
但我不能告诉区别是什么之间302
和303
,或者为什么他们中有谁真正从不同的301
.它似乎302
本来是一个临时重定向,(比如307
),但在实践中,大多数浏览器都把它当成了一个303
.但是a 303
和a 之间有什么区别301
?是301
应该意味着重定向是更永久的吗?
我正在尝试为Express.js应用程序实现更新功能,我想使用PUT请求发送新数据,但我一直在使用PUT收到错误.从我读过的所有内容来看,这只是使用app.put的问题,但这不起作用.我的路线文件中有以下内容:
send = function(req, res) {
req.send(res.locals.content);
};
app.put('/api/:company', function(res,req) {
res.send('this is an update');
}, send);
Run Code Online (Sandbox Code Playgroud)
当我使用邮递员发出PUT请求时,我得到一个"不能PUT/api/petshop"作为错误.我不明白为什么我不能PUT,或者出了什么问题.
好吧我有一个简单的node.js/express.js/mongodb应用程序在这里设置我的配置如下.
var express = require('express'),
mongoose = require('mongoose');
http = require('http');
var app = express();
app.configure(function(){
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
//middleware stack
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(__dirname + "/public"));
});
mongoose.connect("mongodb://localhost/hello");
Run Code Online (Sandbox Code Playgroud)
问题出在我尝试发出PUT或DELETE请求时.我的形式很简单
<form method="POST" action="/users/#{user.name}">
<input type="hidden" name="_method" value="PUT"/>
</form>
Run Code Online (Sandbox Code Playgroud)
现在我的路由器使用express .put()方法捕获路由
app.put('/users/:name', function(req, res) {
var b = req.body;
Users.update(
{ name: req.user.name },
{ name: b.name, age: b.age, email: b.email },
function(err) {
res.redirect('/users/'+b.name);
});
})
Run Code Online (Sandbox Code Playgroud)
当我发出请求时,我只是得到一个"无法PUT"或"无法删除"错误.
我试图通过chomes RESTful客户端以相同的结果发出同样的请求.
我已经阅读了一个与我有同样问题的话题,尽管在评论之后答案并没有解决我的问题.
问题我已经研究了 表达式删除和没有方法覆盖的表达式 …
我一直在搜索如何执行此操作 - 我在尝试DELETE请求后重定向 - 这是我正在使用的代码没有重定向:
exports.remove = function(req, res) {
var postId = req.params.id;
Post.remove({ _id: postId }, function(err) {
if (!err) {
console.log('notification!');
res.send(200);
}
else {
console.log('error in the remove function');
res.send(400);
}
});
};
Run Code Online (Sandbox Code Playgroud)
remove
在删除项目(帖子)时调用.一切正常(我不得不使用res.send(200)
它来挂起删除请求) - 但现在我无法重定向.如果我res.redirect('/forum')
在remove
函数内部使用,像这样:
exports.remove = function(req, res) {
var postId = req.params.id;
Post.remove({ _id: postId }, function(err) {
if (!err) {
console.log('notification!');
res.send(200);
}
else {
console.log('error in the remove function');
res.send(400);
}
res.redirect('/forum'); …
Run Code Online (Sandbox Code Playgroud) 我一直在写我的第一个节点应用程序,一个休息API.我已经把一切都运行得很好但PUT不会经历.我尝试了很多不同的组合,很多旧的教程和一些新的教程,我可能非常接近我只是不确定如何更新记录.此外,如果你看到任何跳出来的东西,请随时告诉我!我真的很喜欢节点,所以我很乐意学习最佳实践.
下面是我的表的路由,所有其他端点工作正常.如果您有任何疑问,请与我联系.
//Database
var mongoose = require("mongoose");
mongoose.connect('mongodb://Shans-MacBook-Pro.local/lantern/');
// Schema
var Schema = mongoose.Schema;
var Person = new Schema({
first_name: String,
last_name: String,
address: {
unit: Number,
address: String,
zipcode: String,
city: String,
region: String,
country: String
},
image: String,
job_title: String,
created_at: { type: Date, default: Date.now },
active_until: { type: Date, default: null },
hourly_wage: Number,
store_id: Number,
employee_number: Number
});
var PersonModel = mongoose.model('Person', Person);
// Return all people
exports.allPeople = function(req, res){
return PersonModel.find(function (err, person) …
Run Code Online (Sandbox Code Playgroud) 在我的快递应用程序中,我有两条路线如下:
router.post('/:date', (req, res) => {
// if date exists, redirect to PUT
// else add to database
})
router.put('/:date', (req, res) => {
// update date
})
Run Code Online (Sandbox Code Playgroud)
如果通话中date
已经存在POST
,我想重定向到PUT
. 使用什么最好的方法来做到这一点res.redirect
?
在docs 中,所有重定向都指向不同的 URL 模式。我想保持 URL 不变,并将其余动词从 更改POST
为PUT
。
我查看了这个SO 问题,并在POST
以下内容中添加了这一行:
res.redirect(303, '/:date');
但它没有将我重定向到PUT
.