Skip to content

some useful es6/es7 untils and api for node.js

Notifications You must be signed in to change notification settings

39Er/node_utils

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

node_utils

some useful es6/es7 untils and api for node.js

Entry

Env

node.js: v7.8.0	
npm: v4.2.0
yarn: v0.24.2 

Basic modules

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地址

Code Styles

Use airbnb eslint code styles

Utils

1. MailSender

Modules:

nodemailer

Config:

{
  "mailConfig": {
    "service": "163",
    // "port": "25",
    // "host": "smtp.163.com",
    "auth": {
      "user": "mailsender999@163.com",
      "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" <mailsender999@163.com>', //  发件人
          	to: '826891502@qq.com',  // 收件人
          	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" <mailsender999@163.com>', //  发件人
        to: '826891502@qq.com',  // 收件人
        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邮箱开启授权码设置

163邮箱设置

2. ChildProcessor

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();
    

About

some useful es6/es7 untils and api for node.js

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published