I have been trying a lot of time about nodejs reading Chinese files. In the end, none of them apply to me. Fortunately, I solved it.
The following three knowledge points are all extracted from the project. To run scripts separately, you need to use global mode to install the module, such as installing the Chinese conversion module (this is also required for other subsequent purposes):
The code copy is as follows:
npm install -g iconv-lite
npm install -g nodemailer
npm install -g node-schedule
1. Issues of nodejs reading Chinese file encoding
Prepare a text file (of course it can also be a csv file, etc.) test.txt and text.csv, and the nodejs file test.js is as follows:
The code copy 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 don’t believe it, you can try it. It is necessary to read it in binary encoding first, and then decode it in GBK. The operation results are as follows:
See more: iconv-lite
2. Nodejs send email
I don't want to say anything, just upload the code, it's easy to understand:
The code copy is as follows:
var nodemailer = require('nodemailer');
//Configure email
var transmitter = nodemailer.createTransport('SMTP',{
service: '163qiye',
auth: {
user: '[email protected]',
pass: '123456',
}
});
//Send email
var sendmail = function(html){
var option = {
from:"[email protected]",
to:"[email protected],[email protected]",
cc:'[email protected]'
}
option.subject = 'The 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 to send email
sendmail("Mail content:<br/>My goal for 2015 is to accomplish the goals of 2014 which I should have done in 2013 because I made a promise in 2012 & planned in 2011!");
See more: nodemailer
3. Nodejs timed tasks
This is used in many ways, and if you are familiar with the linux crontab syntax, it will be even simpler. My Zou's example is very harmonious, haha:
The code copy is as follows:
var schedule = require('node-schedule');
/*Method 1:
Specify a certain time to perform tasks
*/
var schedule = require('node-schedule');
var date = new Date(2014, 12, 31, 16, 1, 0);
var j = schedule.scheduleJob(date, function(){
console.log('2015 will soon come.');
});
/*Method 2:
How many 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 Three:
Similar to crontab
*/
var j = schedule.scheduleJob('1 * * * *', function(){
console.log('It/'s time to afternoon tea!');
});
The operation results are more harmonious, haha:
Don't wonder why the first planned task was not executed, because this is Greenwich time, and it takes 8 hours to execute. Haha~~
See more: node-schedule
After listening to the sad music for a day, I became happier the more I listened. Ha ha