【发布时间】:2018-08-10 20:27:17
【问题描述】:
我遇到了一个问题,我觉得node-http-proxy 正在更改我的目标链接。下面我举了几个例子。
我使用 express 作为我的服务器并使用 Metaweather API。
问题是我能够从下面的端点获取数据 https://www.metaweather.com/api/location/2487956/ https://www.metaweather.com/api/location/2487956/2013/4/30/
但是当我尝试从https://www.metaweather.com/api/location/search/?lattlong=36.96,-122.02调用API时
它以状态码 500 失败,我认为 node-http-proxy 在 122.02 之后添加了一些值,因为它没有被 / 关闭
server.js
const express = require("express");
const next = require("next");
const dev = process.env.NODE_ENV !== "production";
const app = next({ dev });
const handle = app.getRequestHandler();
const httpProxy = require("http-proxy");
const proxyOptions = {
changeOrigin: true
};
const apiProxy = httpProxy.createProxyServer(proxyOptions);
const apiUrl =
"https://www.metaweather.com/api/location/search/?lattlong=36.96,-122.02";
/*
https://www.metaweather.com/api/location/search/?lattlong=36.96,-122.02 - failed with 500
https://www.metaweather.com/api/location/2487956/ - passed
https://www.metaweather.com/api/location/2487956/2013/4/30/ - passed
*/
app
.prepare()
.then(() => {
const server = express();
server.use("/api", (req, res) => {
console.log("Going to call this API " + apiUrl);
apiProxy.web(req, res, { target: apiUrl });
});
server.get("*", (req, res) => {
return handle(req, res);
});
server.listen(3000, err => {
if (err) throw err;
console.log("> Ready on http://localhost:3000");
});
})
.catch(ex => {
console.error(ex.stack);
process.exit(1);
});
感谢您调查此问题。
【问题讨论】:
标签: javascript node.js express http-proxy node-http-proxy