相关疑难解决方法(0)

使用express.js代理

为了避免同域AJAX问题,我希望我的node.js Web服务器将所有请求从URL转发/api/BLABLA到另一个服务器,例如other_domain.com:3000/BLABLA,并且透明地向用户返回该远程服务器返回的相同内容.

所有其他URL(旁边/api/*)将直接提供,不代理.

如何使用node.js + express.js实现此目的?你能给出一个简单的代码示例吗?

(Web服务器和远程3000服务器都在我的控制之下,都运行带有express.js的node.js)


到目前为止,我发现了这个https://github.com/nodejitsu/node-http-proxy/,但阅读那里的文档并没有让我更聪明.我结束了

var proxy = new httpProxy.RoutingProxy();
app.all("/api/*", function(req, res) {
    console.log("old request url " + req.url)
    req.url = '/' + req.url.split('/').slice(2).join('/'); // remove the '/api' part
    console.log("new request url " + req.url)
    proxy.proxyRequest(req, res, {
        host: "other_domain.com",
        port: 3000
    });
});
Run Code Online (Sandbox Code Playgroud)

但没有任何东西返回原始的Web服务器(或最终用户),所以没有运气.

proxy reverse-proxy node.js express

148
推荐指数
7
解决办法
12万
查看次数

使用nodejs的代理

我开发了一个针对api的webapp.由于api没有在我的本地系统上运行,我需要代理请求,所以我不会在跨域问题上运行.有没有一种简单的方法可以做到这一点,所以我的index.html将从本地和所有其他GET,POST,PUT,DELETE请求发送到xyz.net/apiEndPoint.

编辑:

这是我的第一个解决方案,但没有奏效

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) {
    proxy.proxyRequest(req, res, {
        host: 'http://apiUrl',
        port: 80
    });

});
Run Code Online (Sandbox Code Playgroud)

它将提供索引,js,css文件,但不将其余路由到外部api.这是我收到的错误消息:

An error has occurred: {"stack":"Error: ENOTFOUND, Domain name not found\n    at IOWatcher.callback (dns.js:74:15)","message":"ENOTFOUND, Domain name not found","errno":4,"code":"ENOTFOUND"}
Run Code Online (Sandbox Code Playgroud)

proxy node.js express

9
推荐指数
1
解决办法
2万
查看次数

使用 Node.js 后端在我的 React 应用程序中启用 CORS

我使用 create-react-app 来构建我的 React 应用程序。这个应用程序在另一个 API (elasticsearch) 上执行 POST 调用,该 API 托管在不同的服务器上(不是我拥有/管理的)。因此,一旦用户在表单中输入数据,onSubmit 基本上会调用进行调用的 getResponse() 方法。初始化客户端:

let client = new elasticsearch.Client({
    host: "https://{cred.user}:{cred.pass}@@servername.domain:11121",
    log: "trace",
});
Run Code Online (Sandbox Code Playgroud)

API查询:

getResponse = () => {
        client
          .search({
            index: 'custom_index_1',
            body: {
                query: {
                    match: {"data": this.state.data}
                },
            }
        },function(error, response, status) {
            if (error) {
                const errorMessage= {error};
                console.log(errorMessage);
            }
            else {
                this.setState({results: response.hits.hits});
                console.log(this.state.results);
            }
        });
    }
Run Code Online (Sandbox Code Playgroud)

但我收到 CORS 错误如下:

Failed to load resource: the server responded with a status of 403 (Forbidden) …
Run Code Online (Sandbox Code Playgroud)

javascript node.js cors reactjs

8
推荐指数
2
解决办法
1万
查看次数

使用node express的proxy json请求

我使用以下node-express代码来代理从Web服务器到API服务器的请求:

app.use('/api', function(req, res) {
  var url = 'http://my.domain.com/api' + req.url;
  req.pipe(request(url)).pipe(res);
});
Run Code Online (Sandbox Code Playgroud)

这适用于任何动词(get,post等等)的简单请求,但是一旦我发送'Content-type': 'application/json'请求,它就会挂起pipe.

为什么这个简单的node-express代理代码挂在json请求上?
如何改变它来支持它们?

proxy json node.js express

5
推荐指数
1
解决办法
5341
查看次数

标签 统计

node.js ×4

express ×3

proxy ×3

cors ×1

javascript ×1

json ×1

reactjs ×1

reverse-proxy ×1