直接上代碼
sms.js
var http = require('http'); var querystring = require('querystring'); function SmsCode() {  //發(fā)短信  this.send = function (req0, res0)  {   var code = "3212";   var txt = "您的驗(yàn)證碼是:"+code+"。請(qǐng)不要把驗(yàn)證碼泄露給其他人。如非本人操作,可不用理會(huì)!";   var data = {    account: 'myaccount',    password: "mypwd",    mobile:"1370000000",    content:txt   };   data = require('querystring').stringify(data);   console.log(data);   var opt = {    method: "POST",    host: "sms.106jiekou.com",//可以用域名,ip地址    port: 80,    path: "/utf8/sms.aspx",    headers: {     'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'    }   };   var req = http.request(opt, function (res)   {    console.log('STATUS: ' + res.statusCode);    console.log('HEADERS: ' + JSON.stringify(res.headers));    res.setEncoding('utf8');    res.on('data', function (chunk)    {     console.log('BODY: ' + chunk);    });   });   req.on('error', function (e) {    console.log('problem with request: ' + e.message);   });   req.write(data);//把請(qǐng)求發(fā)出去   req.end();  };  //驗(yàn)證碼是否正確  this.verify = function (req, res)  {  } } module.exports = SmsCode;app.js調(diào)用
var Sms = require('sms.js'); var sms = new Sms(); sms.send(req, res); 這樣就行了。
下面給大家補(bǔ)充一個(gè)node.js+express驗(yàn)證碼的實(shí)現(xiàn)
安裝ccap庫 npm install ccap
var ccap = require();var captcha = ccap({  width:190,  height:50,   offset:30,  quality:100,  fontsize:40,  generate:function(){    //自定義生成字符串    //此方法可不要      var str = "qQ";      return str;  }  });var ary = captcha.get();console.log(ary[0]);//字符串res.write(ary[1]); //res.end();以上所述是小編給大家介紹的Nodejs 發(fā)送Post請(qǐng)求功能(發(fā)短信驗(yàn)證碼例子),希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)武林網(wǎng)網(wǎng)站的支持!
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注