如何使用Node.js制作API代理?
由于GFW阻止了包括flickr在内的一些ip,我喜欢使用Node.js创建API代理,但是在http服务器中打开GET请求时遇到了麻烦。这是我的密码:如何使用Node.js制作API代理?,node.js,api,proxy,get-request,Node.js,Api,Proxy,Get Request,由于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(requ
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);
我认为问题在于
response.write(getrequest())代码>
getrequest是一个异步操作,您应该使用回调或promisify。如果使用回调,则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();
}
})
})
然后像这样使用它:
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();
}
})
})
他补充道,我必须写得太快了。