1 Star 1 Fork 0

黄马云/koa-context

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
3.koa.js 1.49 KB
一键复制 编辑 原始数据 按行查看 历史
qqjay2017 提交于 2019-12-01 22:49 . koa-views
const fs = require('fs').promises
const path = require('path')
const uuid = require('uuid')
const Koa = require('koa')
// let bodyParser = require('./koa-bodyparser') //自己的
let bodyParser = require('koa-bodyparser') //别人的
let static = require('./static')
let upload = require('./upload')
/**
*
*/
let app = new Koa()
/**
* 中间件 1.可以扩展一些属性和方法
* 2. 封装中间件
* 扩展一个中间件
* 第一个 将请求参数解析出来
*/
app.use(bodyParser())
/**
* 第二个中间件 静态资源服务
* 如果存在 就讲文件返回
* 不往下走了
*/
app.use(static(path.resolve(__dirname, 'koa')))
/**
* 访问 /form get的时候返回html
*/
app.use(async (ctx, next) => {
if (ctx.path === '/form' && ctx.method.toLowerCase() === 'get') {
// ctx.set('Content-Type', 'text/html;charset=UTF-8')
// ctx.body = fs.createReadStream(path.resolve(__dirname, 'index.html'))
} else {
await next()
}
})
//根据字符串讲buffer切割成数据
Buffer.prototype.split = function (sep) {
let arr = []
let len = Buffer.from(sep).length
let current;
let offset = 0;
while ((current = this.indexOf(sep, offset)) != -1) {
arr.push(this.slice(offset, current));
offset = current + len
}
arr.push(this.slice(offset))
return arr;
}
/**
* 遇到异步 koa中只能使用promise
*
* 访问 /form post的时候
*/
app.use(upload())
app.listen(3000)
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/hhhsir/koa-context.git
git@gitee.com:hhhsir/koa-context.git
hhhsir
koa-context
koa-context
master

搜索帮助

D67c1975 1850385 1daf7b77 1850385