I need to redirect a POST request, and to demonstrate the issue I have setup 2 nodejs servers. One serving some HTML and doing the mod-rewrite and the other receiving the POST after the rewrite. The first one which serves HTML and doing the rewrite looks like this:
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:
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?