webpackHotMiddleware改造成koa支持的中间件
2024-10-20 00:46:18
const stream = require('stream');
const webpackHot = require('webpack-hot-middleware');
const PassThrough = stream.PassThrough;
module.exports = ({compiler, others}) => {
const middleware = webpackHot(compiler, others);
return async (ctx, next) => {
const res = Object.assign({}, ctx.res);
const streamInstance = new PassThrough();
streamInstance.on('data', chunk => {
if(chunk && global.cache &&global.cache.updateCache){
global.cache.updateCache();
}
});
ctx.body = streamInstance;
await middleware(
ctx.req,
Object.assign(res, {
write: streamInstance.write.bind(streamInstance),
writeHead: (
status ,
headers,
) => {
ctx.status = status;
ctx.set(headers);
},
end: (content) => {
ctx.body = content || 'devMiddleWare no body';
},
}),
next
) }
}
最新文章
- UGUI Text(Label)
- Atitit usrQBK13 html dsl 规范与解决方案
- MVC LINQ to SQL: Basic Concepts and Features
- ios学习总结(2) -- UIButton的使用
- jiffies和HZ
- SQL拆分多规则的字符串分离数字。
- RX学习笔记:Bootstrap
- 多校训练赛2 ZCC loves cards
- java中类的三大特征之多态
- 常见的UI框架
- POJ 1704 Georgia and Bob(阶梯Nim博弈)
- 头文件带和不带.h的区别
- Windows7下安装python3.6.3
- vue2.0 之事件处理器
- this语句的用法第一、二点
- Golang 反射reflection
- 【web技术】html特效代码(二)
- 【Git安装】centos安装git
- hdu1224 dp(dp + 栈/父亲数组记录路径)
- google vue开发调试插件,简便安装,亲测可用