简体   繁体   English

使用nodejs的代理

[英]Proxy with nodejs

I develop an webapp, against an api. 我开发了一个针对api的webapp。 As the api is not running on my local system, I need to proxy the request so I dont run in cross domain issues. 由于api没有在我的本地系统上运行,我需要代理请求,所以我不会在跨域问题上运行。 Is there an easy way to do this so my index.html will send from local and all other GET, POST, PUT, DELETE request go to xyz.net/apiEndPoint. 有没有一种简单的方法可以做到这一点,所以我的index.html将从本地和所有其他GET,POST,PUT,DELETE请求发送到xyz.net/apiEndPoint。

Edit: 编辑:

this is my first solution but didnt work 这是我的第一个解决方案,但没有奏效

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
    });

});

It will serve the index, js, css files but dont route the rest to the external api. 它将提供索引,js,css文件,但不将其余路由到外部api。 This is the error message I've got: 这是我收到的错误消息:

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"}

Take a look at the readme for http-proxy . 看一下http-proxy自述文件 It has an example of how to call proxyRequest : 它有一个如何调用proxyRequest

proxy.proxyRequest(req, res, {
  host: 'localhost',
  port: 9000
});

Based on the error message, it sounds like you're passing a bogus domain name into the proxyRequest method. 根据错误消息,听起来好像是在将伪造的域名传递给proxyRequest方法。

Other answers are slightly outdated. 其他答案略显过时。

Here's how to use http-proxy 1.0 with express: 以下是如何使用http-proxy 1.0 with express:

var httpProxy = require('http-proxy');

var apiProxy = httpProxy.createProxyServer();

app.get("/api/*", function(req, res){ 
  apiProxy.web(req, res, { target: 'http://google.com:80' });
});

Here is an example of the proxy which can modify request/response body. 以下是可以修改请求/响应主体的代理示例。

It evaluates through module which implements transparent read/write stream. through实现透明读/写流的模块进行评估。

var http = require('http');
var through = require('through');

http.createServer(function (clientRequest, clientResponse) {

    var departureProcessor = through(function write(requestData){

        //log or modify requestData here
        console.log("=======REQUEST FROM CLIENT TO SERVER CAPTURED========");
        console.log(requestData);

        this.queue(requestData);
    });

    var proxy = http.request({ hostname: "myServer.com", port: 80, path: clientRequest.url, method: 'GET'}, function (serverResponse) {

        var arrivalProcessor = through(function write(responseData){

            //log or modify responseData here
            console.log("======RESPONSE FROM SERVER TO CLIENT CAPTURED======");
            console.log(responseData);

            this.queue(responseData);
        });

        serverResponse.pipe(arrivalProcessor);
        arrivalProcessor.pipe(clientResponse);
    });

    clientRequest.pipe(departureProcessor, {end: true});
    departureProcessor.pipe(proxy, {end: true});

}).listen(3333);

Maybe you should look at my answer here . 也许你应该在这里看看我的答案。 Here I use the request package to pipe every request to a proxy and pipe the response back to the requester. 在这里,我使用请求包将每个请求传递给代理,并将响应传递回请求者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM