如何使用Heroku作为API代理(Node.js)?

时间:2017-06-05 16:18:14

标签: node.js heroku

我尝试使用Heroku作为flickr api的一层,因为GFW阻止了flickr,并且可能在将来阻止其他公共api。我可以在本地环境或AWS上运行index.js,但无法在Heroku上运行。我做了一些研究,看起来Heroku只支持HTTP服务器,因为它有一个dyno之外的层。所以我编写了我的代码如下:

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={{insert_api_key}}&user_id={{insert_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={{insert_api_key}}&user_id={{insert_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);
  });
}

autobuild已完成,但当我向我的dyno网址发送POST请求时,我收到506错误。当我运行heroku run node index.js时,它会抛出以下错误消息:

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)

1 个答案:

答案 0 :(得分:1)

.listen(process.env.PORT || Port)

Heroku需要一个你没有设置的动态PORT。