簡體   English   中英

如何使用Node.js制作API代理?

[英]How to make an API proxy with Node.js?

由於GFW阻止了包括flickr在內的某些ip,因此我希望使用Node.js創建API代理,但是我無法在http服務器中打開GET請求。 這是我的代碼:

var http = require('http');
var qs = require('querystring');
var curl = require('request');
var serverPort = 3000;
http.createServer(function (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 body = getrequest();
        if (body === false) {
          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>');
  }
}).listen(serverPort);
function getrequest(){
  return curl('https://api.flickr.com/services/rest/?method=flickr.photosets.getList&api_key={{insert_ur_api_key}}&user_id={{insert_ur_user_id}}&format=json', function (error, response, body) {
    if (error) {
      return false;
    } else {
      return body;
    }
  });
}
console.log('Server running at localhost:'+serverPort);

在此示例中,我使用了request包(使用npm install request進行安裝)。

這是錯誤消息:

_http_outgoing.js:655
    throw new TypeError('First argument must be a string or Buffer');
    ^

TypeError: First argument must be a string or Buffer
    at write_ (_http_outgoing.js:655:11)
    at ServerResponse.write (_http_outgoing.js:630:10)
    at IncomingMessage.<anonymous> (/Users/aero/Documents/Design/0522-aerofotea api/www/index.js:28:20)
    at emitNone (events.js:105:13)
    at IncomingMessage.emit (events.js:207:7)
    at endReadableNT (_stream_readable.js:1045:12)
    at _combinedTickCallback (internal/process/next_tick.js:102:11)
    at process._tickCallback (internal/process/next_tick.js:161:9)

我認為問題是response.write(getrequest());

getrequest是一個異步操作,您應該使用回調或使它無效化。 如果使用回調,則getrequest函數應如下所示:

function getrequest(callback){
    curl('https://api.flickr.com/services/rest/?method=flickr.photosets.getList&api_key={{insert_ur_api_key}}&user_id={{insert_ur_user_id}}&format=json', (error, response, body)=> {
      if (error) {
          callback(error);
          return;
      } 
      callback(null, body);
    });
}

然后像這樣使用它:

request.on('end', function() {
    getrequest((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();
       }
    })
})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM