About Nodejs read Chinese files really toss a lot of time, online various programs, finally no one applicable to me, fortunately solved.
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 , such as the installation of the Chinese conversion module (the following other needs to do so):
NPM install-g Iconv-lite
NPM install-g Nodemailer
NPM install-g Node-schedule
1, Nodejs reading Chinese file encoding problem
Prepare a text file (which can also be a CSV file, etc.) the test.txt and Text.csv,nodejs files are test.js as follows:
var iconv = require (' iconv-lite '); var fs = require (' FS '); var filestr = Fs.readfilesync (' d:\\test.csv ', {encoding: ' binary '}); var New Buffer (filestr, ' binary '); var str = iconv.decode (buf, ' GBK ');
Console.log (str);
Read the file directly is garbled, do not believe you can try. You need to unify the binary encoding to read it before using GBK decoding. The results of the operation are as follows:
See also:iconv-lite
2, Nodejs send mail
Don't want to say anything, directly on the code, easy to understand:
varNodemailer = require (' Nodemailer '));//Configure mailvarTransporter = Nodemailer.createtransport (' SMTP ', {service:' 163qiye ', auth: {User:' [Email protected] ', pass:' 123456 ', }});//Send mailvarSendMail =function(HTML) {varoption ={from:"[Email protected]", to:"[Email protected],[email protected]", cc:' [Email protected] '} option.subject= ' The five I made 5 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 a messageSendMail ("Mail content:<br/>My goal for are to accomplish the goals of which I should has done in because I made a promise in 2012 & Planned in! ");
See also:nodemailer
3. Nodejs Scheduled Tasks
This is a lot of use, if you are familiar with Linux crontab syntax, this is easier. My Zou's This example is very harmonious, hehe:
varSchedule = require (' Node-schedule '));/*Way One: Specify a time to perform a task*/varSchedule = require (' Node-schedule '));varDate =NewDate (2014, 12, 31, 16, 1, 0);varj = schedule.schedulejob (date,function() {Console.log (' would soon come. ');});/*mode two: How many minutes in each hour to execute*/varRule =Newschedule. Recurrencerule (); Rule.minute= 1;varj = schedule.schedulejob (rule,function() {Console.log (' I\ ' m very happy now! ');});/*Way three: similar to crontab*/varj = Schedule.schedulejob (' 1 * * * * ',function() {Console.log (' it\ ' s time to afternoon tea! ');});
Running results more harmonious, hehe:
Don't wonder why the first scheduled task is not executed, because this is GMT, it needs to wait 8 hours before executing. Hehe ~ ~
See also:node-schedule
Listening to the sad music of the day, the more I hear the more happy. Ha ha
Some dry foods about Nodejs (read Chinese file encoding problem/send mail/timed task)