简体   繁体   中英

How to add proxy to Node / Express site?

My site is running on Node and using the Express framework.

My goal is to gather data from the Yahoo Placefinder api. It does not support JSONP, so I need to send my JQuery.getJSON request to my own proxy. My proxy would then send an http request to the Placefinder api, and echo the response.

If I were using php instead of Node, I would just make a new php file that includes a curl request to the placefinder api and echo the response.

But, I am using Node and I'm not sure where to start.

And, I'm using the Express framework.

My Questions are:

  1. Where would the proxy fit within the Express framework? The public folder?

  2. Where can I find some info on how to code a proxy in Node?

  3. Will I need to modify the configuration of my Rackspace cloud (ubuntu) server in order for this to be possible?

  1. See node-http-proxy . It should be better than implementing your own proxy.

  2. Express lets you add middlewares as arguments when you do express.createServer() . Or, you can add them afterwards by using .use(proxy) .

  3. I don't think so.

To give an example (untested code):

var httpProxy = require('http-proxy'), express = require('express');
var yahooProxy = httpProxy.createServer(80, 'yahoo.com');
var app = express.createServer();
app.configure(function () {
    app.use('/yahoo', yahooProxy);
});

...

Here's another example with 1.0.X that demonstrates header injection.

var express = require( 'express' );
var proxy   = require( 'http-proxy' ).createProxyServer;
var app     = express();

app.configure(function() {

  // Inject some request headers here before we proxy...
  app.use( function( req, res, next ) {
    req.headers[ 'x-my-header' ] = 'blah blah';
    next();
  });

  // Proxy based on path...
  app.use( '/stack', proxy({ target: 'http://stackoverflow.com'} ).web );
  app.use( '/yahoo', proxy({ target: 'http://yahoo.com'} ).web );

  app.use( function( req, res ) {
    res.send({ ok: false, message: 'Not much here.' })
  });

}).listen( 3000 );

You can just add another route to your express app, perhaps at /api/yahoo/... .

This view function will then make a call to the Yahoo API, probably using: http://nodejs.org/docs/v0.4.9/api/http.html#http.request , and then when that request finishes you simple return the result as JSON.

However, keep in mind that your proxy is public and that anyone can make requests through it. I would suggest some basic authorization. A generated value which you provide to the page making the request should work.

Using http-proxy 1.0 with express:

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

var apiProxy = httProxy.createProxyServer();

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

Nowadays this seems to be the easiest solution to add a proxy to Express: https://www.npmjs.org/package/proxy-middleware

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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