gpt4 book ai didi

node.js - 使用快速代理路由没有响应

转载 作者:IT老高 更新时间:2023-10-28 22:00:47 28 4
gpt4 key购买 nike

我用 nodejs、express 和 htt-proxy 编写了一个小型代理。它适用于提供本地文件,但在代理到外部 api 时失败:

var express = require('express'),
app = express.createServer(),
httpProxy = require('http-proxy');


app.use(express.bodyParser());
app.listen(process.env.PORT || 1235);

var proxy = new httpProxy.RoutingProxy();

app.get('/', function(req, res) {
res.sendfile(__dirname + '/index.html');
});
app.get('/js/*', function(req, res) {
res.sendfile(__dirname + req.url);
});
app.get('/css/*', function(req, res) {
res.sendfile(__dirname + req.url);
});

app.all('/*', function(req, res) {
req.url = 'v1/public/yql?q=show%20tables&format=json&callback=';
proxy.proxyRequest(req, res, {
host: 'query.yahooapis.com', //yahoo is just an example to verify its not the apis fault
port: 8080
});

});

问题是yahoo api没有响应,也许有响应但我没有在浏览器中出现。

最佳答案

使用 piperequest-Package

更简单
var request = require('request');

app.use('/api', function(req, res) {
var url = apiUrl + req.url;
req.pipe(request(url)).pipe(res);
});

它将整个请求通过管道传输到 API,并将响应传输回请求者。这也处理 POST/PUT/DELETE 和所有其他请求\o/

如果你也关心查询字符串,你也应该使用管道

req.pipe(request({ qs:req.query, uri: url })).pipe(res);

关于node.js - 使用快速代理路由没有响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7559862/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com