node verification code

'use strict';
const nodemailer = require('nodemailer');
const http = require('http');
const path = require('path');
const fs = require('fs');
const querystring = require('querystring');


const user = {
    name: "[email protected]",
    pass: "ndkcycbbj"
}

// create reusable transporter object using the default SMTP transport
// 使用默认的SMTP传输方式创建可复用的传输对象
let transporter = nodemailer.createTransport({
    host: 'smtp.qq.com',
    port: 465,
    secure: true, // true for 465, false for other ports
    auth: {
        user: user.name, // generated ethereal user
        pass: user.pass // generated ethereal password
    }
});


function getRandom(min, max) {
    return Math.round(Math.random() * (max - min) + min);
}

function sendMail(options) {
    let str = '';
    for (var i = 0; i < 6; i++) {
        var type = getRandom(1, 3);
        switch (type) {
            case 1:
                str += String.fromCharCode(getRandom(48, 57));
                break;
            case 2:
                str += String.fromCharCode(getRandom(65, 90));
                break;
            case 3:
                str += String.fromCharCode(getRandom(97, 122));
                break;
        }
    }

    // 邮件对象

    let mailobj = {
        from: '"root" <[email protected]>', // 发送者
        to: options.email, // 接收者
        subject: '您的验证码到了', // 主题
        html: `<b>验证码是:${str}</b>` // html 内容
    };

    // 发送邮件
    let info = transporter.sendMail(mailobj, (err, data) => {
        if (err) throw err;
        console.log(data);
    });

    return str;
}

//  http服务
http.createServer((req, res) => {
    if (req.url === '/favicon.ico') return;
    res.writeHead(200, { "content-type": "text/html;charset=utf-8" });

    let url = path.join(req.headers.host, req.url);

    let promise = null;

    console.log(`${req.method} ${url}`);

    if (req.method === "GET") {
        switch (req.url) {
            case '/':
                promise = readFile('view', 'reg.html');
                break;
            case '/js/jquery.js':
                promise = readFile('node_modules', 'jquery', 'dist', 'jquery.min.js');
                break;
        }
        promise.then(data => {
            res.end(data);
        }).catch(err => {
            res.end(err);
        });
    } else {
        let data = '';
        req.on('data', chunk => data += chunk);
        req.on('end', () => {
            console.log(data);
            data = querystring.parse(data);
            switch (req.url) {
                case '/mailcode':
                    fs.writeFile(path.join(__dirname, 'temp', data.email), sendMail(data), 'utf8', (err) => {
                        if (err) throw (err);
                        res.end('{ "msg": "邮件已发送" }');
                    });
                    break;
                case '/reg':
                    readFile('temp', data.email).then(function(val) {
                        // console.log(data);
                        if (val == data.code) {
                            res.end('{"msg":"注册成功"}');
                            fs.unlink(path.join(__dirname, 'temp', data.email), err => {
                                if (err) console.log(err);
                            });
                        } else {
                            res.end('{"msg":"验证码错误"}');
                        }
                    }).catch(function(err) {
                        if (err) console.log(err);
                    });

            }
        });
    }
}).listen(8080, () => {
    console.log('服务已经运行 http://localhost:8888');
})

function readFile() {
    return new Promise((resolve, reject) => {
        fs.readFile(path.join(__dirname, ...arguments), 'utf8', (err, data) => {
            if (err) reject(err);
            resolve(data);
        });
    });
}

Guess you like

Origin blog.csdn.net/weixin_45663264/article/details/102709113