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

柳州市诚信体系建设网站广州网站优化推广

柳州市诚信体系建设网站,广州网站优化推广,怎么解决,网站开发培训静态服务中间件:koa-static 中间件可以决定是否向下执行,如果自己可以处理,那么直接处理完毕结束,如果自己处理不了,next 方法会继续向下执行 新建 public 文件夹,里面添加 index.html、style.css 文件 …

静态服务中间件: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.khdw.cn/news/35872.html

相关文章:

  • 建设网站以后百度上做优化
  • wordpress 增加文章字段抖音视频seo霸屏
  • 手机怎么在百度做网站福州seo优化
  • 网站的框架论坛推广怎么做
  • 淘客网站做百度推广百度搜索排名优化哪家好
  • 京东app下载官网青岛seo关键词优化排名
  • 弹幕网站怎么做百度最新财报
  • wordpress旅游类网站如何注册网站怎么注册
  • 做任务网站网络企业推广
  • 做网站一般是什么工作站长工具使用
  • 五个网站页面中国网站排名查询
  • 西安网站空间2021年网络热点舆论
  • 网站的推广费用百度如何收录网站
  • 网站品牌栏目建设情况广州竞价托管代运营
  • 哈尔滨建设网站公司吗天津做优化好的公司
  • 网站找人做备案的价格漯河seo公司
  • 线上培训网站开发南宁seo关键词排名
  • 南京市建筑工程网站新东方培训机构官网
  • 网页设计与制作课程标准构建搜索引擎优化目标
  • 空间站 参考消息上海疫情又要爆发了
  • 通江移动网站建设国际热点新闻
  • 中国政府网站建设与应用游戏优化是什么意思?
  • 网站建设阝金手指科杰十二爱站网关键词长尾挖掘工具
  • 网站建设开发软件有哪些站长工具seo综合查询推广
  • 发布网站后备案襄阳seo优化排名
  • 小程序自助建站百度移动应用
  • 远程教育网站开发网站生成
  • 网络专业有哪些网店产品seo如何优化
  • 怎么知道一个网站是谁做的品牌推广专员
  • wordpress 摘要图片杭州seo搜索引擎优化