使用Express的Node.js:如何重定向POST请求

编程入门 行业动态 更新时间:2024-10-25 20:23:56
本文介绍了使用Express的Node.js:如何重定向POST请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想从一个URL请求重定向到另一个"POST"请求,例如:

I want to redirect from one URL request to another 'POST' request, like this:

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?

However, I can't redirect to '/test' page because it is a POST request. So what should I do to make the redirection work, keeping the '/test' request POST?

推荐答案

您可以执行以下操作:

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

将保留send方法.

作为参考,307 http代码规范为:

For reference, the 307 http code spec is:

307临时重定向(自HTTP/1.1起)在这种情况下,请求 应该与另一个URI重复,但是以后的请求仍然可以使用 原始URI.2与303相比,请求方法不应 重新发出原始请求时进行更改.例如,POST 该请求必须使用另一个POST请求重复进行.

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.

有关更多信息,请参见: www.alanflavell. uk/www/post-redirect.html

For more info, see: www.alanflavell.uk/www/post-redirect.html

更多推荐

使用Express的Node.js:如何重定向POST请求

本文发布于:2023-10-12 00:10:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1483231.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:重定向   Node   Express   POST   js

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!