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

网站做等保备案seo推广技巧

网站做等保备案,seo推广技巧,一流的镇江网站建设,用vue做的网站模板准备工作 安装依赖 npm init -y npm i koakoa 文档:https://koajs.cn/# koa 中不能用回调的方式来实现,因为 async 函数执行的时候不会等待回调完成 app.use(async (ctx, next) > {console.log(ctx.path, ctx.method);if (ctx.path "/login…

准备工作

安装依赖

npm init -y
npm i koa

在这里插入图片描述

koa 文档:https://koajs.cn/#

koa 中不能用回调的方式来实现,因为 async 函数执行的时候不会等待回调完成

app.use(async (ctx, next) => {console.log(ctx.path, ctx.method);if (ctx.path == "/login" && ctx.method === "POST") {const arr = [];ctx.req.on("data", function (chunk) {arr.push(chunk);});ctx.req.on("end", function () {const result = Buffer.concat(arr).toString();console.log("result---->", result);ctx.body = result;});} else {next();}
});

koa 中所有的异步都必须是 promise,只有 promise 才有等待效果,必须所有的 next 方法前需要有 await、return 否则没有等待效果

app.use(async (ctx, next) => {console.log(ctx.path, ctx.method);if (ctx.path == "/login" && ctx.method === "POST") {await new Promise((resolve, reject) => {const arr = [];ctx.req.on("data", function (chunk) {arr.push(chunk);});ctx.req.on("end", function () {const result = Buffer.concat(arr).toString();console.log("result---->", result);ctx.body = result;resolve();});});} else {await next();}
});

实现一个表单提交功能 server.js

const Koa = require("koa");const app = new Koa();app.use((ctx, next) => {// 路径是 /login get 方式// ctx 包含了 request response req resconsole.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") {await new Promise((resolve, reject) => {const arr = [];ctx.req.on("data", function (chunk) {arr.push(chunk);});ctx.req.on("end", function () {const result = Buffer.concat(arr).toString();console.log("result---->", result);ctx.body = result;resolve();});});} else {await next();}
});app.on("error", function (err) {console.log("error----->", err);
});app.listen(3000);

启动服务,访问 http://localhost:3000/login

nodemon server.js

在这里插入图片描述

输入账号密码,点击提交

在这里插入图片描述

koa-bodyparser

下面使用 koa-bodyparser 简化逻辑,安装 koa-bodyparser,https://www.npmjs.com/package/koa-bodyparser

npm i koa-bodyparser

用法:

const Koa = require('koa');
const bodyParser = require('koa-bodyparser');const app = new Koa();
app.use(bodyParser());app.use(async ctx => {// the parsed body will store in ctx.request.body// if nothing was parsed, body will be an empty object {}ctx.body = ctx.request.body;
});

业务里添加逻辑

const Koa = require("koa");
const bodyParser = require("koa-bodyparser");
const app = new Koa();
app.use(bodyParser());app.use((ctx, next) => {// 路径是 /login get 方式// ctx 包含了 request response req resconsole.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);

效果也是一样的

下面自己实现 koa-bodyparser

const querystring = require("querystring");
console.log("使用的是 kaimo-koa-bodyparser 中间件");
// 中间件的功能可以扩展属性、方法
module.exports = function () {return async (ctx, next) => {await new Promise((resolve, reject) => {const arr = [];ctx.req.on("data", function (chunk) {arr.push(chunk);});ctx.req.on("end", function () {if (ctx.get("content-type") === "application/x-www-form-urlencoded") {const result = Buffer.concat(arr).toString();console.log("kaimo-koa-bodyparser-result---->", result);ctx.request.body = querystring.parse(result);}resolve();});});await next(); // 完成后需要继续向下执行};
};

将业务代码的引用自己实现的

// 使用自己实现的 koa-bodyparser
const bodyParser = require("./kaimo-koa-bodyparser");

启动服务,效果一样:

在这里插入图片描述

http://www.hkea.cn/news/333497/

相关文章:

  • 沈阳招聘网站开发地推项目平台
  • 798艺术区成都seo达人
  • 平度网站建设抖音代运营收费详细价格
  • 株洲网站优化找哪家seo优化的价格
  • 找印度人做网站sem竞价推广公司
  • 山西网站推广公司网站关键词优化怎么弄
  • 微信分销是什么重庆优化seo
  • 武汉企业网站推广方案永久免费无代码开发平台网站
  • 网站开发岗位群怎样推广产品
  • 桐城市美丽乡村建设专题网站石家庄整站优化技术
  • 北京建网站的公司哪个比较好郑州seo价格
  • 进空间的网站网络营销常见的工具
  • wordpress发文章的id怎么不连续如何做好搜索引擎优化工作
  • 交互式网站如何做seo推广软件排名
  • 西部建设网站惠州seo排名优化
  • 做环球资源网站有没有效果2024百度下载
  • 织梦 安装网站网站搭建需要多少钱
  • 做网站购买备案域名性价比高seo的排名优化
  • 潍坊中脉网站建设公司淄博seo公司
  • 深圳做网站推广哪家好青岛关键词优化平台
  • 呼和浩特市网站建设公司uc搜索引擎入口
  • 网站怎么做关键词搜索电子商务主要学什么内容
  • python做的网站漏洞百度竞价推广开户联系方式
  • 做任务换流量的网站怎么自己制作网页
  • 福清建设局网站火蝠电商代运营公司
  • 爱玖货源站在线智能识图
  • 上海网络营销软件windows优化大师win10
  • 专做美妆的视频网站ui设计
  • 平度市建设局网站济宁百度推广价格
  • 茶类网站建设方案西安网站seo排名优化