Recently, I have been studying the full stack development of NodeJS + Express +mysql. I know that mysql and Express are old now, but I feel they are mature and familiar with them. Let’s learn about them first and then take a look at Kao and MongoDB. Here I use NodeJS and Tencent cloud SMS SDK development of the verification code service, god mouth mercy ah ~.

demand

The verification code must be sent using NodeJS. The same mobile phone number 180s can only be requested once.

Analysis of the

Step by step (analyzing requirements has become a habit ^ _ ^)

  1. Check whether the mobile phone number was sent within 180s. If yes, the mobile phone number can only be sent once within 180s. Otherwise, the verification code is recalculated.
  2. If the phone number has never been issued, calculate the verification code.
  3. Store the verification code.
  4. Send the verification code.
  5. Verify that the mobile phone number and verification code in the registration data are consistent.

implementation

Send verification code

First look at the flow chart:

Using the Express Session package, install and use the following:

npm install express-session --save
Copy the code

Configuration:

app = express();
app.use(session({
        secret: '610481', 
        resave: false, 
        saveUninitialized: true,
        cookie: {
            maxAge: 1000*60*30
        },
        rolling:true
    })
);
Copy the code

Code: Just an example

SelectPhone :(req,res,next) => {// jump from route to let phone = req.body.phone, // get the post parameter "phone number" romStr; Session [phone]){if(req. Session [phone]){ If ((date.parse (new Date()) -req.session [phone][1])/1000 > 180){romStr = RndNum(4); req.session[phone] = [romStr,Date.parse(new Date())]; // store session}else{res.json({MSG :"180 seconds only one! ") }}else{romStr = RndNum(4); req.session[phone] = [romStr,Date.parse(new Date())]; }; Pool.getconnection (function(err, connection) {connection.query(userSQL. SelectPhone, phone, function(err, Result) {// Check whether the mobile phone number is registered. JudageRes is a self-written method for processing the data found in the database. After processing, return to the callback. Common. judgeRes(result,'query',err,res,function(data){if(data.data[0]){data.data = 'Phone number registered! Ssender (phone,romStr,(req,res,resData)=>{if (err) {console.log("err: ", err);}else{sms.ssender(phone,romStr,(req,res,resData)=>{if (err) {console.log("err: ", err); Data. data = "Failed to send verification code!" } else { console.log("request data: ", res.req); console.log("response data: ", resData); Data. data = "Verification code sent successfully!" }}}})); // Release connection.release(); }); }); }Copy the code

Tencent Cloud SMS NodeJS SDK package:

var QcloudSms = require("qcloudsms_js"); // SMS app SDK AppID var AppID = XXXXXXX; AppKey var AppKey = "XXXXXXX "; Var templateId = 7839 in the SMS application. // NOTE: templateId 7839 is only an example, the real templateId needs to be obtained in the SMS console //templateId 7839 corresponds to "{1}" var smsSign = "XX"; QcloudSms var QcloudSms = QcloudSms(appid, appkey); var ssender = qcloudsms.SmsSingleSender(); module.exports = { /* * @params phoneNumbers [Array or String] * @params params [Array] * @params callback(req res resData) */ ssender:(phoneNumbers,params,callback) => { ssender.sendWithParam(86, phoneNumbers,templateId,params,smsSign, "", "", callback); }}Copy the code

pit

Because postman was used for interface test, I always came to the stage where phone did not exist. After a long time of struggle, I finally found that response would be set-cookie. Postman is not a browser and will not carry this in the next request, so I had to manually set the Cookie during the test.

This is the responseconnect.sid



Set cookies on request:

If you have other ways, please guide.