gpt4 book ai didi

带有 Express : how to redirect a POST request 的 Node.js

转载 作者:IT老高 更新时间:2023-10-28 22:11:35 33 4
gpt4 key购买 nike

我想从一个 URL 请求重定向到另一个 'POST' 请求,如下所示:

var app = require('express')();

app.get('/', function(req, res) {
res.redirect('/test');
});

app.post('/test', function(req, res) {
res.send('/test page');
});

app.listen(3000, function() {
console.log('listenning on port:3000');
});

但是,我无法重定向到“/test”页面,因为它是一个 POST 请求。
那么我应该怎么做才能使重定向工作,保持'/test'请求POST?

最佳答案

你可以这样做:

app.post('/', function(req, res) {
res.redirect(307, '/test');
});

这将保留发送方法。

供引用,307 http 代码规范为:

307 Temporary Redirect (since HTTP/1.1) In this occasion, the request should be repeated with another URI, but future requests can still use the original URI.2 In contrast to 303, the request method should not be changed when reissuing the original request. For instance, a POST request must be repeated using another POST request.

有关详细信息,请参阅:http://www.alanflavell.org.uk/www/post-redirect.html

关于带有 Express : how to redirect a POST request 的 Node.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38810114/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com