Nodejs Send Post Request Function of Send SMS Verification Code Example

  • 2021-07-18 06:57:09
  • OfStack

Direct code

sms.js


var http = require('http'); 
var querystring = require('querystring'); 
function SmsCode() 
{ 
 // Send a text message  
 this.send = function (req0, res0) 
 { 
  var code = "3212"; 
  var txt = " Your verification code is: "+code+" . Please don't reveal the verification code to others. If it is not my operation, don't pay attention! "; 
  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",// You can use domain names ,ip Address  
   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);// Send a request  
  req.end(); 
 }; 
 // Is the verification code correct  
 this.verify = function (req, res) 
 { 
 } 
} 
module.exports = SmsCode;

app. js call


var Sms = require('sms.js'); 
var sms = new Sms(); 
sms.send(req, res); 

That's it.

The following is a supplement to the implementation of node. js+express verification code

Install the ccap library npm install ccap


var ccap = require();
var captcha = ccap({
        width:190,
        height:50,   
        offset:30,
        quality:100,
        fontsize:40,
        generate:function(){
                  // Custom build string 
                  // Do not use this method 
      var str = "qQ";
      return str;
        }
        
});
var ary = captcha.get();
console.log(ary[0]);// String 
res.write(ary[1]); //
res.end();

Related articles: