直接上代码
sms.js
var http = require('http'); var querystring = require('querystring'); function SmsCode() { //发短信 this.send = function (req0, res0) { var code = "3212"; var txt = "您的验证码是:"+code+"。请不要把验证码泄露给其他人。如非本人操作,可不用理会!"; 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);//把请求发出去 req.end(); }; //验证码是否正确 this.verify = function (req, res) { } } module.exports = SmsCode;
app.js调用
var Sms = require('sms.js'); var sms = new Sms(); sms.send(req, res);
这样就行了。
下面给大家补充一个node.js+express验证码的实现
安装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 发送Post请求功能(发短信验证码例子),希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!