Skip to content

Instantly share code, notes, and snippets.

@leeight
Created March 15, 2016 08:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leeight/c7928d68aeef88c00f93 to your computer and use it in GitHub Desktop.
Save leeight/c7928d68aeef88c00f93 to your computer and use it in GitHub Desktop.
var http = require('http');
var url = require('url');
var util = require('util');
var Auth = require('bce-sdk-js').Auth;
var kCredentials = {
ak: '您的AK',
sk: '您的SK'
};
http.createServer(function (req, res) {
var query = url.parse(req.url, true).query;
var statusCode = 200;
var signature = null;
if (!query.httpMethod || !query.path || !query.params || !query.headers) {
statusCode = 403;
}
else {
var httpMethod = query.httpMethod;
var path = query.path;
var params = safeParse(query.params) || {};
var headers = safeParse(query.headers) || {};
// 添加您自己的额外逻辑
var auth = new Auth(kCredentials.ak, kCredentials.sk);
signature = auth.generateAuthorization(httpMethod, path, params, headers);
}
var payload = {
statusCode: statusCode,
signature: signature,
xbceDate: new Date().toISOString().replace(/\.\d+Z$/, 'Z')
};
res.writeHead(statusCode, {'Content-Type': 'text/javascript; charset=utf-8'});
if (query.callback) {
res.end(util.format('%s(%s)', query.callback, JSON.stringify(payload)));
}
else {
res.end(JSON.stringify(payload));
}
}).listen(1337);
console.log('Server running at http://0.0.0.0:1337/');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment