当前位置: 首页 > news >正文

83 # 静态服务中间件 koa-static 的使用以及实现

静态服务中间件:koa-static

中间件可以决定是否向下执行,如果自己可以处理,那么直接处理完毕结束,如果自己处理不了,next 方法会继续向下执行

新建 public 文件夹,里面添加 index.html、style.css 文件

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><title>凯小默测试静态服务中间件koa-static</title><link rel="stylesheet" href="./style.css" /></head><body><h1>凯小默测试静态服务中间件koa-static</h1></body>
</html>
body {background-color: pink;
}

koa-static

npm i koa koa-static

用法:

const serve = require('koa-static');
const Koa = require('koa');
const app = new Koa();// $ GET /package.json
app.use(serve('.'));// $ GET /hello.txt
app.use(serve('test/fixtures'));// or use absolute paths
app.use(serve(__dirname + '/test/fixtures'));app.listen(3000);console.log('listening on port 3000');

业务代码 static.js 中使用 koa-static

const Koa = require("koa");
const path = require("path");
const bodyParser = require("koa-bodyparser");
// 使用自己实现的中间件
// const static = require("koa-static");
const static = require("./kaimo-koa-static");
const app = new Koa();
app.use(bodyParser());
app.use(static(__dirname));
app.use(static(path.resolve(__dirname, "public")));app.use((ctx, next) => {console.log(ctx.path, ctx.method);if (ctx.path == "/login" && ctx.method === "GET") {ctx.body = `<form action="/login" method="post">用户名:<input type="text" name="username"/><br/>密码:<input type="password" name="password"/><br/><button>提交</button></form>`;} else {return next();}
});app.use(async (ctx, next) => {console.log(ctx.path, ctx.method);if (ctx.path == "/login" && ctx.method === "POST") {ctx.body = ctx.request.body;} else {await next();}
});app.on("error", function (err) {console.log("error----->", err);
});app.listen(3000);

启动服务,访问 http://localhost:3000/index.html

在这里插入图片描述

nodemon static.js

下面实现自己的 koa-static,需要安装 mime

const path = require("path");
const fs = require("fs").promises;
const mime = require("mime");console.log("使用的是 kaimo-koa-static 中间件");
module.exports = function static(root) {return async (ctx, next) => {let filePath = path.join(root, ctx.path);try {let statObj = await fs.stat(filePath);// 判断是否是文件if (statObj.isFile()) {ctx.type = mime.getType(filePath) + ";charset=utf-8";ctx.body = await fs.readFile(filePath);} else {await next();}} catch (e) {await next();}};
};

在这里插入图片描述

在这里插入图片描述

http://www.lryc.cn/news/166593.html

相关文章:

  • 带讲解的自行车租赁系统,可做毕设/课设
  • mysql指令
  • 【C语言】每日一题(半月斩)——day2
  • 电脑如何查看代理服务器IP?
  • 【C++11】{}初始化、std::initializer_list、decltype、STL新增容器
  • 【FPGA项目】进阶版沙盘演练——报文收发(报文处理、CDC、CRC)
  • 【程序员装机】自定义Edge浏览器用户目录
  • ubuntu18、20 cv_bridge 与自带opencv版本冲突问题
  • 贝叶斯分位数回归、lasso和自适应lasso贝叶斯分位数回归分析免疫球蛋白、前列腺癌数据...
  • css自学框架之图片懒加载
  • RoutingKafkaTemplate,DefaultKafkaProducerFactory和 ReplyingKafkaTemplate
  • Flutter动态化开发之Fair实战
  • Stream流编程
  • jenkins自动化脚本集成时钉钉消息未发送
  • java面试题第七天
  • MATLAB入门-矩阵的运算
  • [X3m]ros交叉编译
  • 【漏洞库】Fastjson_1.2.47_rce
  • zabbix 钉钉微信企微告警(动作操作消息内容模板)
  • 阿里云国际站云服务器数据备份方法有哪些?
  • 游戏笔记本电脑可以进行 3D 建模和渲染吗?有哪些优势与缺点?
  • 【AI】推理系统和推理引擎的整体架构
  • k8s集群中流水线部署微服务
  • Socks5代理与网络安全:保护您的隐私与数据
  • 2024年山东高企申报注意事项
  • npm publish包报404,is not in the npm registry错误
  • pytest-基础
  • 在openSUSE上开启护眼模式
  • vue基础知识十:Vue中组件和插件有什么区别?
  • Arthas是一个Java诊断工具 的入门使用