NodeUtil
This is just a tool collect for easy use of some tool library. I will pack some operation for more easy to use.
Install
npm install nodeutil
Usage
var nu = require('nodeutil');
Using logger
var log = nu.logger.getInstance();
log.info('Test logger...');
Or you can insert log name to getInstance() to catelogry your log...
var log = nu.logger.getInstance('MAIN_LOG');
You can also set logger some advance properties:
var log = require('nodeutil').logger.getInstance('io_sockets', {
path: 'your-logger-path',
catg: 'log-category-name',
level: 'log-level',
logMaxSize: 'max-log-file-size',
logBackup: 'backup-days'
});
Advance Logger
If your need to do more with log, you can extend the log4js setting like this:
var log = nu.logger.addInstance('appjs', {
"type": "dateFile",
"filename": 'test-log.log',
"pattern": "-yyyy-MM-dd-ss.log",
"alwaysIncludePattern": true
});
In addAppender(), you can put appender config using log4js config format. Related config format, please reference to log4js: https://github.com/nomiddlename/log4js-node
For date rolling log, please reference here for dateFile appender: https://github.com/nomiddlename/log4js-node/wiki/Date-rolling-file-appender
Simple Log
Simple log is fix format using js file name and function name as prefix. It is focus that user can easily find the log exist in
var log = require('nodeutil').simplelog;
log.info('Hello %s!');
Using dateutil
var dateutil = no.dateutil;
var pattern = 'yyyymmdd hh24:mi:ss';
dateutil.getNowString(pattern);
dateutil.getDateString(new Date(), pattern)
Using step
var Step = nu.step;
Step(
function step1(){
//do something...
return something1;
},
function step2(step1_returned){
//do something
return something2;
},
...
)
Using cfgutil
var cfgutil = nu.cfgutil;
//read from json file
var cfg = cfgutil.readJsonCfg('path to json config file');
Using mailutil
var mailer = nu.mailutil;
mailer.init(
{"smtpOptions":{"service":"Gmail", "auth": {"user": "your-account","pass": "your-password"}}, "sender": "NO-REPLY <no-reply@micloud.tw>"}
);
mailer.sendNodeMailAsync('receiver@gmail.com',
'test mail send...',
'send mail OK!',
true,
function(){
console.log('Send mail done...');
}
);
Using mailutil through localhost sendmail service
var mailer = require('nodeutil').mailutil;
mailer.init(
{"smtpOptions":{"host":"localhost"}, "sender": "NO-REPLY <no-reply@micloud.tw>"}
);
mailer.sendNodeMailAsync('yourmail@gmail.com',
'test mail send...',
'send mail OK!',
true,
function(){
console.log('Send mail done...');
}
);
Advance using mailutil...
mailer.init(
{"smtpOptions": {"service":"Gmail",
"auth": {
"user": "your_mail_username", "pass": "your_password"
}},
"sender": "NO-REPLY <no-reply@your.mail.address>"
}
);
mailer.sendNodeMail({
to:["receiver1@gmail.com"],
subject: "test123",
html:"<h1>TEST123</h1>",
cc:["cc-receiver@gmail.com"],
attachments: [
{
fileName: "text1.txt",
contents: "hello world!"
},
{
fileName: "text2.txt",
contents: new Buffer("hello world!","utf-8")
}]
},
true, function(res){
console.log(res);
}
);
Convert Json to Table
var json2table = nu.json2table;
var json = [{aaa:123, bbb:223}, {aaa:223, bbb:323}];
console.log(json2table.ConvertJsonToTable(json));
The result:
<table border="1" cellpadding="1" cellspacing="1"><thead><tr><th>aaa</th><th>bbb</th></tr></thead><tbody><tr><td>123</td><td>223</td></tr><tr><td>223</td><td>323</td></tr></tbody></table>
Generate GUID
var guid = nu.guid;
console.log( guid.getGuid('%s%s%s', 0,0));
console.log( guid.getSimpleGuid('%s-%s-%s', 2,3));
var a = '12341234'
console.log( guid.getSimpleGuidWithSeed(a, '%s-%s-%s', 2,3));
The result:
-13ee608f7e0-
FE-13ee608f7e1-Dia
rm-12341234-eep