Node.js 与 Express:如何重定向 POST 请求

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38810114/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 20:28:09  来源:igfitidea点击:

Node.js with Express: how to redirect a POST request

node.jsredirectexpress

提问by neolicd

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

我想从一个 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');
});

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?

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

回答by ldg

You can do this:

你可以这样做:

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

Which will preserve the send method.

这将保留发送方法。

For reference, the 307 http code spec is:

作为参考,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.

307 Temporary Redirect (因为HTTP/1.1) 这种情况下,请求应该用另一个URI重复,但以后的请求仍然可以使用原来的URI。2 与303相反,重新发出原始请求时不应改变请求方法. 例如,必须使用另一个 POST 请求重复 POST 请求。

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

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

回答by Radagast the Brown

Keep in mind the middleware architecture: Each handler may manipulate the context, and either respond - or - call next().

请记住中间件架构:每个处理程序都可以操作上下文,并且可以响应或调用next()

By this premise, the express router is basically a middleware function you may use after "correcting" the url.

在这个前提下,express router 基本上是一个中间件功能,你可以在“更正”url 后使用。

(BTW, the request app is also a function, although I'm not sure if I recommend going back so early in the chain)

(顺便说一句,请求应用程序也是一个功能,虽然我不确定我是否建议在链的早期返回)

Here's a kind'a example:

这是一个例子:

const router = new require('express').Router()
const user = require('../model/user') 
//assume user implements:
//  user.byId(id) -> Promise<user>
//  user.byMail(email) -> Promise<user>

const reqUser = userPromise => (req, res, next) =>
   req.user
     ? next()
     : userPromise(req)
       .then(user => { req.user = user })
       .then(next, next)
//assume the sever that uses this router has a 
//standard (err, req, res, next) handler in the end of the chain...

const byId = reqUser( req => user.byId(req.params.id) )
const byMail = reqUser( req => user.byMail(req.params.mail) )

router.post('/by-id/:id/friends',
  byId,
  (req, res) => res.render('user-friends', req.user)
)

router.post('/by-email/:email/friends',
  byMail,
  (req, res, next) => {
     req.url = `/by-id/${req.user.id}/friends`
     next()
  }, 
  router
)

回答by pshx

The only difference between 307 and 302 is that 307 guarantees that the method and the body will not be changed when the redirected request is made.

307 和 302 的唯一区别是 307 保证在进行重定向请求时不会更改方法和主体。

https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/307

https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/307