简体   繁体   English

connect-modrewrite 将我的 POST 请求更改为 GET

[英]connect-modrewrite changes my POST request into a GET

I need to redirect a POST request, and to demonstrate the issue I have setup 2 nodejs servers.我需要重定向 POST 请求,并演示我设置了 2 个 nodejs 服务器的问题。 One serving some HTML and doing the mod-rewrite and the other receiving the POST after the rewrite.一个提供一些 HTML 并进行 mod-rewrite,另一个在重写后接收 POST。 The first one which serves HTML and doing the rewrite looks like this:第一个提供 HTML 并进行重写的代码如下所示:

var connect = require('connect');
var cors = require('cors');
const modRewrite = require('connect-modrewrite')

var http = require('http');
const { nextTick } = require('process');

var app = connect();

app.use(cors())


app.use('/test', function(req, res, next){
    res.end(`
        <!doctype html><html><body>

        <button type="button"class="yes">click</button>

        <script>
            async function postData (url = '', data = {}) {
                const response = await fetch(url, {
                    method: 'POST',
                    mode: 'cors',
                    cache: 'no-cache', 
                    credentials: 'same-origin',
                    headers: {
                        'Content-Type': 'application/json',
                        'Access-Control-Allow-Origin': '*',
                        'Access-Control-Allow-Methods': 'DELETE, POST, GET, OPTIONS'
                    },
                    redirect: 'follow', // manual, *follow, error
                    referrerPolicy: 'no-referrer', // no-referrer, *client
                    body: JSON.stringify(data)
                })
                return await response.json();
              }

              document.querySelector('.yes').addEventListener('click', () => {
                postData('http://localhost:3000/a/b', { answer: 42 }).then(data => {
                    console.log(data); 
                });
            });
        </script></body></html>`);
    });

    app.use(function(req, res, next){
        console.log('RECEIVED', req.method);
        next();
    });

    app.use(modRewrite([
        '^/(.*)$ http://localhost:4001/$1 [R, L]'
    ])
);

http.createServer(app).listen(3000);

So the line '^/(.*)$ http://localhost:4001/$1 [R, L]' rewrites the POST to my other backend server:所以行'^/(.*)$ http://localhost:4001/$1 [R, L]'将 POST 重写到我的另一个后端服务器:

var connect = require('connect')
var cors = require('cors')
var http = require('http')

var app = connect()
app.use(cors());
app.use(function (req, res) {
   console.log('RECEIVED', req.url, req.method);
  res.end(JSON.stringify({status: 'ok'}));
});

http.createServer(app).listen(4001

So the flow is as follows:所以流程如下:

1) In the browser I do a fetch/POST
2) It is received as POST and connect-modrewrite is applied
3) My second backend prints: RECEIVED /a/b GET

So my question is, why is my POST transformed into a GET and how can I fix this?所以我的问题是,为什么我的 POST 转换为 GET,我该如何解决这个问题?

This is expected behavior.这是预期的行为。 When you do a redirect (301/302), POST data is discarded on redirect as a client will perform a GET request to the URL specified by the 301.当您执行重定向 (301/302) 时,POST 数据在重定向时会被丢弃,因为客户端将对 301 指定的 URL 执行 GET 请求。

In order to fix this, you should use redirect 307. See this for reference: https://softwareengineering.stackexchange.com/questions/99894/why-doesnt-http-have-post-redirect#99966为了解决这个问题,您应该使用重定向 307。请参阅此参考: https : //softwareengineering.stackexchange.com/questions/99894/why-doesnt-http-have-post-redirect#99966

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM