首页 > 解决方案 > Node JS 反向代理 - POST 抛出 404 错误

问题描述

我有一个以下节点 js 服务器:

var express  = require('express');
var app      = express();
var httpProxy = require('http-proxy');
var apiProxy = httpProxy.createProxyServer();
var serverOne = 'http://<address>:<port>/sap/opu/odata/SAP/Z_ATTENDANCE_SRV/';

app.use(express.static('webapp'));
app.use(express.static('./'));

app.get("/*", function(req, res) {
    console.log('redirecting to Server1');
    apiProxy.web(req, res, {target: serverOne});
});
app.listen(3000);

当使用 GET 请求时,localhost:3000/AttendanceSet它工作正常。但是当我调用 POST 请求时,localhost:3000/AttendanceSet我收到以下错误:

The following problem occurred: HTTP request failed404,Not Found,<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>Cannot POST /AttendanceSet</pre>
</body>
</html>
 -  

我应该如何更改服务器的代码以使其工作?谢谢你。

标签: node.jsodatareverse-proxy

解决方案


尝试使用app.all,因此您可以使用相同的处理程序处理所有不同的 HTTP 动词。

app.all("/*", function(req, res) {
    console.log('redirecting to Server1');
    apiProxy.web(req, res, {target: serverOne});
});

推荐阅读