some useful es6/es7 untils and api for node.js
node.js: v7.8.0
npm: v4.2.0
yarn: v0.24.2
config: for configuration github地址
eslint: check code styles github地址
mocha: unit test github地址
should.js: BDD style assertions for node.js github地址
bunyan: log libary form node.js github地址
lodash: A modern JavaScript utility library delivering modularity, performance & extras. github地址
Use airbnb eslint code styles
Modules:
Config:
{
"mailConfig": {
"service": "163",
// "port": "25",
// "host": "smtp.163.com",
"auth": {
"user": "[email protected]",
"pass": "******" // 163邮箱需使用授权码作为密码,其他邮箱待测
}
},
"mailMaxRetry": 5
}
Example:
-
async/await
const EmailSender = require('./lib/EmailSender'); let sendEmailAsync = async () => { let emailSender = new EmailSender(); let result = {}; const mail = { from: '"MailSender" <[email protected]>', // 发件人 to: '[email protected]', // 收件人 cc: '', // 抄送 subject: 'Test mail sender', // 主题 text: 'Plaintext version of the message', // 正文 // html: '<p>HTML version of the message</p>' // html正文 }; try { result = await emailSender.sendMail(mail); console.log(result); } catch (e) { console.error(e); } }; sendEmailAsync();
-
Promise
let emailSender = new EmailSender(); const mail = { from: '"MailSender" <[email protected]>', // 发件人 to: '[email protected]', // 收件人 cc: '', // 抄送 subject: 'Test mail sender', // 主题 text: 'Plaintext version of the message', // 正文 // html: '<p>HTML version of the message</p>' // html正文 }; emailSender.sendMail(mail) .then((result) => { console.log(result); }) .catch((error) => { console.error(error); });
Attention: 163邮箱开启授权码设置
Effectively prevent Windows Chinese garbled
Methods:
- exec()
- execFile()
- spawn()
参数详见node:child_process
对于child_process方法的选择详见:我的blog
Example:
-
exec()
const ChildProcessor = require('./lib/ChildProcessor'); let exec = async () => { let cp = new ChildProcessor(); try { let result = await cp.exec('git status'); console.log(result); } catch (e) { console.log(e); } }; exec();
-
execFile()
const ChildProcessor = require('./lib/ChildProcessor'); let execFile = async () => { let cp = new ChildProcessor(); try { let result = await cp.execFile('node', ['-h']); console.log(result.stdout); } catch (e) { console.log(e); } }; execFile();
-
spawn()
const ChildProcessor = require('./lib/ChildProcessor'); let spawn = async () => { let cp = new ChildProcessor(); try { let result = await cp.spawn('cmd', ['/c', 'dir']); console.log(result.stdout); } catch (e) { console.log(e); } }; spawn();