https://github.com/laomu1988/koa2-extend
koa@next的一些扩展函数
https://github.com/laomu1988/koa2-extend
koa koa2
Last synced: 10 months ago
JSON representation
koa@next的一些扩展函数
- Host: GitHub
- URL: https://github.com/laomu1988/koa2-extend
- Owner: laomu1988
- Created: 2017-03-17T11:55:28.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2017-03-22T02:59:36.000Z (over 9 years ago)
- Last Synced: 2025-03-04T06:18:18.910Z (over 1 year ago)
- Topics: koa, koa2
- Language: JavaScript
- Size: 8.79 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# koa@next的一些扩展函数
要求node版本>=6.0.0
## install
```
npm install koa2-extend
```
## usage
```
var koa = require('koa');
require('koa2-extend'); // 要在实例化app前引入koa2-extend
var app = new koa();
app.use(function(ctx) {
console.log('fullUrl:',ctx.fullUrl);
if(ctx.url.indexOf('json') >= 0) {
ctx.response.body = '{"encode":true}';
console.log(ctx.response.json.encode);
// 将输出: true
}
else {
ctx.response.body = 'test';
var $ = ctx.response.$;
$('body').addClass('test');
ctx.response.body = $.html();
console.log(ctx.response.body);
// 将输出: test
}
});
```
## api
* ctx.fullUrl 同request.fullUrl
* ctx.isBinary(filepath) 根据文件路劲判断是否是二进制文件
* ctx.sendFile(filepath) 发送文件
* ctx.isDir(path) 判断本地路径是否是文件夹
* request.fullUrl 包含protocol和host的完全链接
* response.$ 使用cheerio包裹后的html对象, 使用时转码, 转换失败返回''
* response.json 使用JSON.parse转换body后的对象, 使用时转换, 转换失败返回''