Installing middleware
CD Project file
NPM Install HTTPS
NPM Install MD5
Code:/
/* If called directly, add the return function callback */
var express = require (' Express ');
var router = Express. Router ();
var MD5 = require (' MD5 '); //MD5 Module
var https = require (' https '); The//https module is used to send asynchronous requests (Request sub accounts)
function Creatsid (callback) {
var usermod = db.get (' user ');
var accountsid = "accountsid";
var key = "key";
Build time because the timestamp generated by the new date is different from the requirements of the Jong API interface, so you combine
var dates = new Date ();
var year = dates.getyear ()%100+2000;
var month = pad (dates.getmonth () +1,2);
var day = pad (Dates.getdate (), 2);
var hours = pad (dates.gethours (), 2);
var second = pad (Dates.getseconds (), 2);
var minute = pad (Dates.getminutes (), 2);
var time = year+ "" +month+ "" +day+hours+minute+second;
//Generate Base64 format Authorization string
Base64 (accountsid+ ":" +time) time is a combination of the previously
var b = new Buffer (accountsid+ ":" +time);
var s = b.tostring (' base64 ');
Generate an encrypted string sig
var sigparameter = MD5 (accountsid+key+time). toUpperCase ();
var Authorization = s;
var phone = ' 18065741234 '; Jong account username (not Chinese)
Post Data packet
var data = {' appId ': ' aaf98f894c983f52014c9c0a4a3a0279 ', ' FriendlyName ':p hone};
Go to JSON string
data = json.stringify (data);
Post path Path (after domain name)
var url = "/2013-12-26/accounts/" +accountsid+ "/subaccounts?sig=" +sigparameter;
Post parameter settings
var opt = {
method: "POST",
Host: "Sandboxapp.cloopen.com",
port:8883,
Path:url,
headers: {
' Accept ': ' Application/json ',
' content-type ': ' Application/json;charset=utf-8 ',
' content-length ':d ata.length,
authorization:authorization
}
};
var req = https.request (opt, function (serverfeedback) {//Establish POSTL link
//res.send (Serverfeedback.statuscode);
if (serverfeedback.statuscode = = 200) {
var body = "";
serverfeedback.on (' Data ', function (data) {body + = data;}). On (' End ', function () {//Get Return Data
BODY = json.parse (body);
if (Body.statuscode = = 000000) {
return callback (body);
}
}
Else {
return callback ( "error");
}
});
req.write (data); Send a post packet
req.end (); Send End
}
Called by the Router method
Router.get ('/', function (req, res, next) {
Creatsid (function (res) {
Res.send (RES);
});
});
Module.exports = router;
Nodejs Express Jong Communication Generation sub Account