node-annotation开发的初衷是因为node本身在服务层面并不太成熟,而且代码层次及维护各大公司都有自己的做法,借鉴于Java里spring的想法开发了这个,当然在功能实现上和spring相差甚多,这里只实现了几个重要功能,欢迎有志同道合者共同来维护。
注解区别于正常可运行的代码,仅仅是注释的写法,这里写法格式为:
/*@注解名称(注解参数)*/
注解参数支持格式:
- 单引号或者双引号括起来的字符串
- JSON格式
纳入node-annotation管理 如果需要使用node-annotation来处理模块,需要在扫描文件完成后才能做业务操作
var nodeAnnotation = require('node-annotation');
/** 配置全局错误处理
* [Function] 错误处理函数
*/
nodeAnnotation.setGlobalErrorHandler(function(err){
console.error(err);
})
/** 配置node-annotation内的日志流出
* [Boolean] 是否开启日志,默认true
* [String] "error/warn/info/log" 输出日至级别,默认warn
* [Function/LoggerObject] 日志处理函数或对象(类似log4js的Logger对象),默认为console
*/
nodeAnnotation.setLogger(true, 'info', function(str, level) {
console.log('[NodeAnnotation]', str);
});
/* 配置资源路径
* [String] 配置文件所在路径(文件夹),默认工程目录下的resoucres文件夹
*/
nodeAnnotation.configurePath(path.join(process.cwd(), 'resources'));
nodeAnnotation.start(源文件路径, function() {
nodeAnnotation.app(/*你的express app*/)
//你的业务代码
});