Authored by 周奇琪

初始化

node_modules/
... ...
facebook和谷歌服务代理
> node app.js
\ No newline at end of file
... ...
var express = require('express');
var app = express();
var fs = require('fs');
app.use(function(req, res, next){
console.log('%s %s', req.method, req.url);
next();
});
app.use(express.static(__dirname + '/public'));
app.use(express.bodyParser());
//index
app.get('/',function(req,res)
{
var html = '<!doctype html><html><head><title>U Up Up!</title> <meta name="google-site-verification" content="cQoYR2ncKEPvT919ZOGe_-X34kitFX8ijFoJ1sdAnA0" /><link rel="stylesheet" type="text/css" href="http://fonts.googleapis.com/css?family=Lemon"> <head><body style="background:#000 url(http://farm8.staticflickr.com/7292/8825786798_883bee009d_o.jpg) center 0 no-repeat;color:#fff;background-size:100%"><h1 style="font-family:Lemon;text-align:center">U Up Up!</h1></body></html>'
res.setHeader('Content-Type','text/html');
res.setHeader('Content-length',html.length);
res.end(html);
});
var FB = require('fb');
app.get('/fb',function(req,res)
{
var data = req.param('data');
data = JSON.parse(data);
var method = data.method;
var fields = {fields:data.fields};
if(data.limit)
{
fields.offset = data.offset;
fields.limit = data.limit;
}
var at = data.accessToken
FB.setAccessToken(at);
console.log(fields);
if(data.fields)
{
FB.api(method,fields,function (resp)
{
console.log(resp);
res.send(resp);
});
}
else
{
FB.api(method,function (resp)
{
console.log(resp);
res.send(resp);
});
}
});
app.post('/fb',function(req,res)
{
var data = req.body.data;
data = JSON.parse(data);
console.log(data);
var method = data.method;
var body = data.body;
var at = data.accessToken
FB.setAccessToken(at);
FB.api(method,'post',body,function (resp)
{
console.log(resp);
res.send(resp);
});
});
app.listen(80);
console.log('OK!');
... ...
var Stream = require('stream').Stream;
var util = require('util');
module.exports = DelayedStream;
function DelayedStream() {
this.source = null;
this.dataSize = 0;
this.maxDataSize = 1024 * 1024;
this.pauseStream = true;
this._maxDataSizeExceeded = false;
this._released = false;
this._bufferedEvents = [];
}
util.inherits(DelayedStream, Stream);
DelayedStream.create = function(source, options) {
var delayedStream = new this();
options = options || {};
for (var option in options) {
delayedStream[option] = options[option];
}
delayedStream.source = source;
var realEmit = source.emit;
source.emit = function() {
delayedStream._handleEmit(arguments);
return realEmit.apply(source, arguments);
};
source.on('error', function() {});
if (delayedStream.pauseStream) {
source.pause();
}
return delayedStream;
};
DelayedStream.prototype.__defineGetter__('readable', function() {
return this.source.readable;
});
DelayedStream.prototype.resume = function() {
if (!this._released) {
this.release();
}
this.source.resume();
};
DelayedStream.prototype.pause = function() {
this.source.pause();
};
DelayedStream.prototype.release = function() {
this._released = true;
this._bufferedEvents.forEach(function(args) {
this.emit.apply(this, args);
}.bind(this));
this._bufferedEvents = [];
};
DelayedStream.prototype.pipe = function() {
var r = Stream.prototype.pipe.apply(this, arguments);
this.resume();
return r;
};
DelayedStream.prototype._handleEmit = function(args) {
if (this._released) {
this.emit.apply(this, args);
return;
}
if (args[0] === 'data') {
this.dataSize += args[1].length;
this._checkIfMaxDataSizeExceeded();
}
this._bufferedEvents.push(args);
};
DelayedStream.prototype._checkIfMaxDataSizeExceeded = function() {
if (this._maxDataSizeExceeded) {
return;
}
if (this.dataSize <= this.maxDataSize) {
return;
}
this._maxDataSizeExceeded = true;
var message =
'DelayedStream#maxDataSize of ' + this.maxDataSize + ' bytes exceeded.'
this.emit('error', new Error(message));
};
... ...
{
"name": "proxy",
"version": "1.0.0",
"description": "facebook和谷歌服务代理",
"main": "app.js",
"dependencies": {
"express": "^4.12.0",
"fb": "0.7.0",
"request": "2.53.0"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "h1bomb"
}
... ...