代码拉取完成,页面将自动刷新
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)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。