This article mainly introduces the problem of reading Chinese file encoding, sending emails and scheduled task instances in Nodejs. This article uses three modules to solve these three requirements and provides code operation examples, for more information about how to read Chinese files from node. JS, it took a lot of time to read Chinese files. There were no solutions available for me on the Internet.
The following three knowledge points are extracted from the project. To run the script separately, you need to use the global mode to install the module, for example, installing the Chinese conversion module (you also need to do this in the future ):
The Code is as follows:
Npm install-g iconv-lite
Npm install-g nodemailer
Npm install-g node-schedule
1. Code for nodejs to read Chinese files
Upload a file (of course, it can also be csvfile, such as )test.txtand text.csv). The nodejs file test. js is as follows:
The Code is as follows:
Var iconv = require ('iconv-lite ');
Var fs = require ('fs ');
Var fileStr = fs. readFileSync ('d: \ test.csv ', {encoding: 'binary '});
Var buf = new Buffer (fileStr, 'binary ');
Var str = iconv. decode (buf, 'gbk ');
Console. log (str );
If you read the file directly, it is garbled. If you do not believe it, try it. You must first read the data in binary encoding and then use GBK to decode the data. The running result is as follows:
For more information, see iconv-lite.
2. send an email to nodejs
I don't want to talk about anything. I can directly use the code, which is easy to understand:
The Code is as follows:
Var nodemailer = require ('nodemailer ');
// Configure the email
Var transporter = nodemailer. createTransport ('smtp ',{
Service: '163qiye ',
Auth :{
User: 'zhoujie0111 @ 126.com ',
Pass: '20140901 ',
}
});
// Send an email
Var sendmail = function (html ){
Var option = {
From: "zhoujie0111@126.com ",
To: "zhoujie0111@126.com, zhoujie0111@126.com ",
Cc: 'zhoujie0111 @ 126.com'
}
Option. subject = 'five-Year Plan I made five years ago'
Option.html = html;
Transporter. sendMail (option, function (error, response ){
If (error ){
Console. log ("fail:" + error );
} Else {
Console. log ("success:" + response. message );
}
});
}
// Call to send an email
Sendmail ("Mail content:
My goal for 2015 is to accomplish the goals of 2014 which I shoshould have done in 2013 because I made a promise in 2012 & planned in 2011! ");
For more information, see nodemailer.
3. node. js scheduled task
This method is widely used. If you are familiar with the linux crontab syntax, it is simpler. This example of my workshop is very harmonious:
The Code is as follows:
Var schedule = require ('node-schedule ');
/* Method 1:
Task execution at a specific time
*/
Var schedule = require ('node-schedule ');
Var date = new Date (2014, 12, 31, 16, 1, 0 );
Var j = schedule. scheduleJob (date, function (){
Console. log ('2017 will soon come .');
});
/* Method 2:
The number of minutes per hour.
*/
Var rule = new schedule. RecurrenceRule ();
Rule. minute = 1;
Var j = schedule. scheduleJob (rule, function (){
Console. log ('I \'m very happy now! ');
});
/* Method 3:
Similar to crontab
*/
Var j = schedule. scheduleJob ('1 ***** ', function (){
Console. log ('It \'s time to afternoon tea! ');
});
The running results are more harmonious:
Don't wonder why the first scheduled task is not executed, because it is Greenwich Mean Time and it needs to wait 8 hours before execution. Haha ~~
For more information, see node-schedule.
After listening to the sad music of a day, the more happy you are. Haha