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

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

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

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

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

课程章节: 6-7 API对接mysql(博客详情和新建)

课程讲师: 双越

课程内容:
修改项目 blog-1 文件夹。
对 博客详情 和 新建 进行修改
./src/controller/blog.js

/* 
  数据层
*/
const { exec } = require("../db/mysql");


// 获取博客详情
const getDetail = (id) => {
  const sql = `select * from blogs where id='${id}'`;

  return exec(sql).then((rows) => {
    // 返回一个对象
    return rows[0];
  });
};

// 新建一篇博客
const newBlog = (blogData = {}) => {
  // blogData 是一个博客对象,包含 title content author 属性
  const title = blogData.title;
  const content = blogData.content;
  const author = blogData.author;
  const createTime = Date.now();

  const sql = `
      insert into blogs (title, content, createTime, author ) 
      value ('${title}', '${content}', ${createTime}, '${author}')
    `;

  return exec(sql).then((insertData) => {
    // console.log("insertData is", insertData);
    return {
      id: insertData.insertId,
    };
  });
};


module.exports = {
  getDetail,
  newBlog,
};

./src/router/blog.js

const {
  getDetail,
  newBlog,
} = 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/detail") {
    /* const data = getDetail(id);
    return new SuccessModel(data, "这是获取博客详情的接口"); */
    const result = getDetail(id);
    return result.then((data) => {
      return new SuccessModel(data, "这是获取博客详情的接口");
    });
  }

  // 新建一篇博客
  if (method == "POST" && req.path === "/api/blog/new") {
    /*  const data = newBlog(req.body);
    return new SuccessModel(data, "这是新建博客的接口"); */

    // 假数据,在开发登录时再改成真数据
    req.body.author = "zhangsan";

    const result = newBlog(req.body);
    return result.then((data) => {
      return new SuccessModel(data, "这是新建博客的接口");
    });
  }

};

module.exports = handleBlogRouter;

课程收获:
从mysql获取博客详情和新建博客,有一定的了解
图片描述

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

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

評(píng)論

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

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

100積分直接送

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

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

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

購課補(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
提交
取消