第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

【金秋打卡】第7天 Node.js+Express+Koa2 開(kāi)發(fā)Web Server博客 6-6

標(biāo)簽:
征文 活動(dòng)

课程名称: 2022全新 Node.js+Express+Koa2 开发Web Server博客

课程章节: 6-6 API对接mysql(博客列表)

课程讲师: 双越

课程内容:
修改项目 blog-1 的文件夹。
对获取博客数据的修改,从mysql获取数据

//  ./src/controller/blog.js 文件

const { exec } = require("../db/mysql");

// 获取博客列表
const getList = (author, keyword) => {
  // 1=1(正确) 起到站位作用
  let sql = `select * from blogs where 1=1 `;

  if (author) {
    sql += `and author='${author}' `;
  }

  if (keyword) {
    sql = `and title like '%${keyword}%' `;
  }

  // 倒序排序
  sql += `order by createtime desc;`;

  // 返回 promise
  return exec(sql);
};
module.exports = {
  getList,
};

修改 ./src/routers/blog.js 文件,获取博客列表的接口

// ./src/routers/blog.js
const {
  getList,
} = require("../controller/blog.js");

const { SuccessModel, ErrorModel } = require("../model/resModel.js");

// 博客相关接口
const handleBlogRouter = (req, res) => {
  const method = req.method; // GET POST
  const id = req.query.id;

  // 获取博客列表
  if (method == "GET" && req.path === "/api/blog/list") {
    const author = req.query.author || "";
    const keyword = req.query.keyword || "";
	
    const result = getList(author, keyword);
    // 返回 promise
    return result.then((listData) => {
      return new SuccessModel(listData, "这是获取博客列表的接口");
    });
  }

};

module.exports = handleBlogRouter;

修改app文件中博客路由的返回值

// ./app.js
const qs = require("qs");

// 博客相关接口
const handleBlogRouter = require("./src/router/blog.js");

// 用于处理 post data
const getPostData = (req) => {
  return new Promise((resolve, reject) => {
    if (req.method !== "POST") {
      resolve({});
      return;
    }

    if (req.headers["content-type"] !== "application/json") {
      resolve({});
      return;
    }

    let postData = "";
    req.on("data", (chunk) => {
      postData += chunk.toString();
    });

    req.on("end", () => {
      if (!postData) {
        resolve({});
        return;
      }

      resolve(JSON.parse(postData));
    });
  });
};

const serverHandle = (req, res) => {
  // 设置返回格式
  res.setHeader("Content-type", "application/json");

  // 处理 path
  const url = req.url; // 获取路由
  req.path = url.split("?")[0]; // 获取api

  // 解析 query
  req.query = qs.parse(url.split("?")[1]);

  // 处理post data
  getPostData(req).then((postData) => {
    // 保存 post 方式传递的数据
    req.body = postData;

    // 处理 blog 路由
    const blogResult = handleBlogRouter(req, res);
    if (blogResult) {
      blogResult.then((blogData) => {
        res.end(JSON.stringify(blogData));
      });

      return;
    }

};

module.exports = serverHandle;

// process.env.NODE_ENV

课程收获:
前端调用一个接口,后端如何从mysql获取博客列表数据,的一个流程有一定的了解
图片描述

點(diǎn)擊查看更多內(nèi)容
TA 點(diǎn)贊

若覺(jué)得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

正在加載中
  • 推薦
  • 評(píng)論
  • 收藏
  • 共同學(xué)習(xí),寫(xiě)下你的評(píng)論
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說(shuō)多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開(kāi)微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊(cè)有機(jī)會(huì)得

100積分直接送

付費(fèi)專欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消