Nodejs read Chinese file encoding problem, send mail and timed task instance _node.js

Source: Internet
Author: User
Tags crontab syntax

About Nodejs read Chinese file really toss a lot of time, online various programs, finally no one applies to me, fortunately solved.

The following three knowledge points are extracted from the project, to run the script separately, you need to install the module in a global mode, such as installing the Chinese Translation module (and so on for the rest):

Copy Code code as follows:

NPM install-g Iconv-lite

NPM install-g Nodemailer

NPM install-g Node-schedule

1, Nodejs read Chinese file encoding problem

Prepare a text file (which can also be a CSV file, etc.) Test.txt and Text.csv,nodejs files test.js as follows:

Copy Code code 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);

Directly read the file is garbled, do not believe you can try. It needs to be uniformly read in binary encoding and then decoded with GBK. The results of the operation are as follows:

More See also: Iconv-lite

2, Nodejs send mail

Do not want to say, directly on the code, easy to understand:

Copy Code code as follows:

var Nodemailer = require (' Nodemailer ');

//Configure mail
var transporter = nodemailer.createtransport (' SMTP ', {
    service: ' 163qiye ',
    Auth: {
        User: ' zhoujie0111@126.com ',
        pass: ' 123456 ',
   }
};

//Send Message
var sendmail = function (html) {
    var option = {
     & nbsp;      from: "Zhoujie0111@126.com",
             to: "Zhoujie0111@126.com,zhoujie0111@126.com",
             cc: ' zhoujie0111@126.com '
       }
    option.subject = ' Five-year plan 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 Send Message
SendMail ("Mail content: <br/>my goal for 2015 are to accomplish the goals of 2014 which I should Have do in 2013 because I made a promise AT & planned in 2011! ");

More See also: Nodemailer

3, Nodejs Timing task

This is a lot of usage, and if you are familiar with the Linux crontab syntax, this is much simpler. This example of my Zou is very harmonious, hehe:

Copy Code code as follows:

var schedule = require (' Node-schedule ');

/* Mode one:
Specify a time to perform a task
*/
var schedule = require (' Node-schedule ');
var date = new Date (2014, 12, 31, 16, 1, 0);

var j = schedule.schedulejob (date, function () {
Console.log (' 2015 'll soon come. ');
});

/* Mode two:
How many minutes per hour are executed
*/
var rule = new schedule. Recurrencerule ();
Rule.minute = 1;
var j = schedule.schedulejob (rule, function () {
Console.log (' I\ ' m very happy now! ');
});

/* Mode three:
Similar crontab
*/
var j = schedule.schedulejob (' 1 * * * * *, function () {
Console.log (' it\ ' time to afternoon tea! ');
});

Running results more harmonious, hehe:

Don't wonder why the first scheduled task didn't execute because it was GMT and it took 8 hours before it was executed. Oh ~ ~

More See also: Node-schedule

Listening to the sad music of the day, the more I hear the happier. Ha ha

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.