Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js expressjsproxy调用webapi_Node.js_Express_Proxy Server - Fatal编程技术网

Node.js expressjsproxy调用webapi

Node.js expressjsproxy调用webapi,node.js,express,proxy-server,Node.js,Express,Proxy Server,我有以下代码。以及一个返回字符串数组的web api const express = require('express'); const proxy = require('express-http-proxy'); var app = express(); app.use('/proxy', proxy('http://localhost:56660/api/values')); app.listen(3000); 当我尝试执行localhost:3000/proxy时,没有得到响应,

我有以下代码。以及一个返回字符串数组的web api

const express = require('express');
const proxy = require('express-http-proxy');


var app = express();

app.use('/proxy', proxy('http://localhost:56660/api/values'));

app.listen(3000);
当我尝试执行localhost:3000/proxy时,没有得到响应, 但是当我使用app.use('/proxy',proxy('www.google.com'),它重定向到谷歌网站

请向我推荐一个最佳方法/解决方案:
我想创建一个代理服务器,从浏览器(应用程序)获取url,修改url,调用新url并将响应发送回浏览器(应用程序)。

您可以将url作为查询参数进行代理,对其进行修改,然后将该url传递给
代理程序,如下所示(使用而不是
应用程序。使用('/proxy',proxy('http://localhost:56660/api/values);
):

您可以使用如下URL调用服务器(GET方法):

更新:

app.use('/proxy', proxy('http://localhost:56660/api/values', {
  proxyReqPathResolver: function(req) {
    const requestedUrl = `${req.protocol}://${req.get('Host')}${req.url}`
    const modifiedURL = modifyURL(requestedUrl)
    return require('url').parse(modifiedURL).path;
  }
}))
我认为这将根据您的需要进行:

app.use('/proxy', (req, res, next) => {
    const requestedUrl = `${req.protocol}://${req.get('Host')}${req.url}`
    const modifiedURL = modifyURL(requestedUrl)
    proxy(modifiedURL)(req, res, next)
})
更新2:

app.use('/proxy', proxy('http://localhost:56660/api/values', {
  proxyReqPathResolver: function(req) {
    const requestedUrl = `${req.protocol}://${req.get('Host')}${req.url}`
    const modifiedURL = modifyURL(requestedUrl)
    return require('url').parse(modifiedURL).path;
  }
}))
更新3:

app.use('/proxy', proxy('http://localhost:56660/api/values', {
  proxyReqPathResolver: function(req) {
    const requestedUrl = `${req.protocol}://${req.get('Host')}${req.url}`
    const modifiedURL = modifyURL(requestedUrl)
    return require('url').parse(modifiedURL).path;
  }
}))
代理修改响应()的示例


如果不将url作为查询参数传递,我可以做类似的事情吗?您希望如何将其传递给服务器?我已经尝试了将url作为查询参数传递给服务器的解决方案,但它仍然没有显示任何输出。它说无法访问站点。我希望代理服务器获取url并通过更改域名和添加一些额外的查询参数来修改它设置并调用修改后的url,但不将url作为查询参数传递。我想我现在了解了您需要什么,请参阅上面的修改代码。