Nodejs implementation, automatic daily reading of database data-Generate Excel form-Send to Boss Mailbox (promise version)

Source: Internet
Author: User
Tags install node npm install node

Async Version: http://blog.csdn.net/zzwwjjdj1/article/details/52129192write this version is mainly, promise more useful, and, now is the nodejs of the built-in objects, no need to reference third-party library-The tools neededMySQL Database
Connection database module MySQL Basic package: http://blog.csdn.net/zzwwjjdj1/article/details/51991348
Auto-run module Node-schedule basic use: http://blog.csdn.net/zzwwjjdj1/article/details/51898257
Generate Excel Table Node-xlsx
Send mail Nodemailer basic use: http://blog.csdn.net/zzwwjjdj1/article/details/51878392
--Installation ModuleI set up a folder on the F drive: Automation
NPM install MySQL
NPM Install Node-schedule
NPM Install Node-xlsx
NPM Install Nodemailer
--Encapsulation of 3 methods1. Accessing the database
2. Send mail

3. Format time

Promise.js

/* Tool Class August 22, 2016 09:07:50 */var db = {};  var mysql = require (' mysql '); var Nodemailer = require (' Nodemailer '); var pool = Mysql.createpool ({connectionlimit:10, host: ' Local  Host ', User: ' Root ', password: ' 123456 ', database: ' Nodejs '}); Executes the SQL statement and returns the result db.query = function (SQL) {return new Promise (function (resolve, reject) {if (!sql) {Reject (          ' Pass parameter Error! ');      Return          } pool.query (SQL, function (err, rows, fields) {if (err) {Console.log (err);        Reject (err) return;        };    Resolve (rows);    }); })};//send mail with attachment Db.sendmail = function (xlsxname) {var transporter = Nodemailer.createtransport ({service: ' QQ ', a  Uth: {User: ' [email protected] ', pass: ' Drckgvaniifuxxxx '}}); var mailoptions = {from: ' [email protected] ', to: ' [email protected] ', Subject: ' Boss, you want Excel to come, format yourself Deal with it! ', HTML: ' 

--Test codeAuto.js
var xlsx = require (' node-xlsx '), var schedule = require ("Node-schedule"), var mysql = require ('./promise '); var fs = require ( ' FS '); var auto = function () {mysql//queries the data and translates to the format required to generate xlsx. Query (' SELECT * from Users '). Then (function (rows) {var datas = [] Rows.foreach (function (row) {var newRow = [];for (var key in row) {Newrow.push (Row[key]);} Datas.push (NewRow);}) Return Promise.resolve (datas);}) Generate xlsx file. Then (function (datas) {var buffer = Xlsx.build ([{name: "Today's revenue", Data:datas}]); var xlsxname = ' ${mysql.nowdate (). Split (') [0]}.xlsx '; return new Promise (function (resolve, reject) {fs.writefile (xlsxname, buffer, ' binary ', function (Err) {if (err) {throw new error (' Create Excel exception '); return;} Resolve (Xlsxname)})})//Send message, return information. Then (function (xlsxname) {return mysql.sendmail (xlsxname);}). Then (function (info) {Console.log (info);}) Catches unhandled exceptions. catch (function (e) {console.log (e);}); var rule = new schedule. Recurrencerule (); var t = [];for (var i = 0; i <; i++) {T.push (i);} var times = T;rule.second = Times;schedulE.schedulejob (rule, function () {Auto ();}); 
--runto see the effect, the automatic execution is set to 1 seconds to execute once.
-- mail--
-- specific content- -
-- printing information--
--Database--
--excel--

Nodejs implementation, automatic daily reading of database data-Generate Excel form-Send to Boss Mailbox (promise version)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.