Monday 5 June 2017

How to use Heroku as API proxy?

I try to use Heroku as a layer of for flickr api because GFW has blocked flickr, and perhaps other public api in the future. I can run the index.js on a local environment or on AWS, but failed to run on Heroku. I did a bit of research and it looks like Heroku only support HTTP server as it has a layer outside of the dyno. So I wrote my code as followed:

var http = require('http');
var qs = require('querystring');
var curl = require('request');
var Port = 80;
var httpServer = http.createServer(app).listen(Port);

function app(request, response) {
  if(request.method === "GET") {
    response.writeHead(401, {'Content-Type': 'text/html'});
    response.write('<!doctype html><html><head><title>401</title></head><body>401: Unauthorized</body></html>');
    response.end();
  } else if(request.method === "POST") {
    if (request.url === "/") {
      var requestBody = '';
      request.on('data', function(data) {
        requestBody += data;
        if(requestBody.length > 1e7) {
          response.writeHead(413, 'Request Entity Too Large', {'Content-Type': 'text/html'});
          response.end('413: Request Entity Too Large');
        }
      });
      request.on('end', function() {
        var formData = qs.parse(requestBody);
        if (formData.request === 'list') {
          var url = 'https://api.flickr.com/services/rest/?method=flickr.photosets.getList&api_key=&user_id=&format=json';
        } else if (formData.request === 'photoset') {
          var url = 'https://api.flickr.com/services/rest/?method=flickr.photosets.getPhotos&photoset_id=' + formData.id + '&api_key=&user_id=&extras=url_o&format=json';
        } else {
          response.writeHead(400, 'Bad Request', {'Content-Type': 'text/html'});
          response.end('400: Bad Request');
          return;
        }
        getrequest(url, (err, body)=>{
          if (err) {
            response.writeHead(502, 'Bad Gateway', {'Content-Type': 'text/html'});
            response.end('502: Bad Gateway');
          } else {
            response.writeHead(200, {'Content-Type': 'text/html'});
            response.write(body);
            response.end();
          }
        });
      });
    } else {
      response.writeHead(404, 'Resource Not Found', {'Content-Type': 'text/html'});
      response.end('<!doctype html><html><head><title>404</title></head><body>404: Resource Not Found</body></html>');
    }
  } else {
    response.writeHead(405, 'Method Not Supported', {'Content-Type': 'text/html'});
    return response.end('<!doctype html><html><head><title>405</title></head><body>405: Method Not Supported</body></html>');
  }
};
function getrequest(url, callback){
  curl(url, (error, response, body) => {
    if (error) {
      callback(error);
      return;
    }
    callback(null, body);
  });
}

The autobuild completed but I get 506 error when I sent POST request to my dyno's url. When I run heroku run node index.js it throws the following err message:

events.js:160
      throw er; // Unhandled 'error' event
      ^

Error: listen EACCES 0.0.0.0:80
    at Object.exports._errnoException (util.js:1018:11)
    at exports._exceptionWithHostPort (util.js:1041:20)
    at Server._listen2 (net.js:1245:19)
    at listen (net.js:1294:10)
    at Server.listen (net.js:1390:5)
    at Object.<anonymous> (/app/index.js:5:41)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)



via Aero Wang

No comments:

Post a Comment