koa-core
the util library for koa web application
Modules
Errors
there are some Object exports which all extends Error, they have the same property all type
and the value are as the same as the object's name,
e.g.
var err = 'err message';console;console;// ApiRequestError// err message
-
ApiRequestError
-
DBError
-
PageError
it has the
status
property which stands for httpStatus, the same as http status code, default500
var err = 'err message' 404;console;console;// 404// err message -
CodeException
fileHelper
the util for handle files
-
##### has(uri)
check the path exits?
uri
: the path (string)return true or false
-
##### write(uri, content)
write something into the file
uri
: the file path (string)content
: the file content (string) -
##### read(uri, def)
read file content
uri
: the file path (string)def
: if readfile fail or file unexits (string)return string
-
##### remove(uri)
remove file
uri
: the file path (string) -
##### readJSON(uri, def)
read the json file content and return a json object
uri
: the file path (string)def
: the default return when the reading fail (object) -
##### mkdirSync(dirname)
mkdir
dirname
: dirname
http
the module for http request which extend request
https://github.com/request/request/blob/master/README.md
-
##### get(url, params, header) the request with
get
methodurl
: request URLparams
: request paramsheader
: request header, default{}
return promise
-
##### post(url, params, header) the request with
get
methodurl
: request URLparams
: request paramsheader
: request header, default{}
return promise
there are some method you can register to global
// 注册请求前事件http {} // 格式化输出http { return res; } // 判断请求是否成功http { return Numberrescode === 1; } // 注册请求完成事件, 无论成功与否http {} // 业务上的错误回调http {} httpdefaultOptions = time: true;//option 结构参考 https://github.com/request/request/blob/master/README.md
logger
use:
var logger = logger;logger
-
##### log(msg, type)
add logs content
msg
: logs content (string)type
: log type which from your conf, defaultlog
(string)
there are some method you can register to global
logger { }
- #####
logger.Logger
is the constructor which you can extend, the only arg was the option the can see https://github.com/node-modules/mini-logger#options
PageHelper
the helper for view engin
-
##### contructor(options)
- options default was { jsonFileName: 'staticAsset.json' }
-
##### setCss(moduleName)
get css url from the webpack asset.json file by moduleName
-
##### setScript(moduleName)
get js url from the webpack asset.json file by moduleName
-
##### setImgSrc(srcPath)
set the webpack build img path
usage with
img-assets-webpack-plugin
-
##### isToday(date)
...
-
##### formatDate (dateString, format)
...
-
##### fromNow (dataString)
// 1 天前// 4 小时前 -
##### getweek (dateString)
format the date to
周x