Skip to content

Instantly share code, notes, and snippets.

@takimo
Created October 4, 2011 00:11
Show Gist options
  • Select an option

  • Save takimo/1260598 to your computer and use it in GitHub Desktop.

Select an option

Save takimo/1260598 to your computer and use it in GitHub Desktop.
mixi_nodejs
/*
* mixi nodejs
* @author shinya.takimoto
*/
// require
var http = require('http');
var dispatcher = require('./dispatcher.js').create();
var https = require('https');
var querystring = require('querystring');
// constants
var MIXI_TOKEN_ENDPOINT = 'https://secure.mixi-platform.com/2/token';
var MIXI_API_ENDPOINT = 'http://api.mixi-platform.com/2';
// application data
var CONSUMER_KEY = "";
var CONSUMER_SECRET = "";
var REDIRECT_URL = "http://localhost:8080/callback";
var SCOPE = ['r_profile'];
var getAuthorizeURL = function(){
var params = querystring.stringify({
'client_id' : CONSUMER_KEY,
'renseponse_type' : 'code',
'scope' : SCOPE.join(' '),
'display' : 'pc',
});
var url = MIXI_TOKEN_ENDPOINT + "?" + params;
return url;
};
var getTokenFromCode = function(code){
return token;
};
http.createServer(function (req, res) {
dispatcher.guide(req, res);
}).listen(8080, "localhost");
dispatcher.get('/', function(req, res){
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end(getAuthorizeURL());
});
dispatcher.get('/callback', function(req, res){
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello Callback\n');
});
console.log('Server running at http://localhost:8080/');
/*
* 簡易ルーティング
* @author shinya.takimoto
*/
var dispatcher = (function(){
var Klass = function(){
this.initialize();
};
(function(){
this.initialize = function(){
this._routings = {
'get' : [],
'post' : []
};
};
this.get = function(rule, callback){
this._routings['get'].push({
'rule': rule,
'callback': callback
});
};
this.post = function(rule, callback){
this._routings['post'].push({
'rule': rule,
'callback': callback
});
};
this.guide = function(request, response){
var callback = null;
var method = request.method.toLowerCase();
var path = require('url').parse(request.url).href;
for(var i=0;i<this._routings[method].length; i++){
var rule = this._routings[method][i].rule;
var reg = new RegExp("^" + rule + "$");
if(reg.test(path)){
callback = this._routings[method][i].callback;
}
};
if(callback) {
callback(request, response);
}else{
response.writeHead(404, {'Content-Type': 'text/plain'});
response.end('');
}
};
}).apply(Klass.prototype);
return Klass;
})();
exports.create = function(){
return new dispatcher();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment