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

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

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

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

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

课程章节: 6-9 API对接mysql(登录)

课程讲师: 双越

课程内容:
修改项目 blog-1 文件夹。
对 博客的登录进行 修改
./src/controller/user.js

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

const loginCheck = (username, password) => {
  const sql = `
    select username, realname from users where username='${username}' and password='${password}'
  `;

  return exec(sql).then((rows) => {
    return rows[0] || {};
  });
};

module.exports = {
  loginCheck,
};

./src/router/user.js

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

// 登录相关接口
const handleUserRouter = (req, res) => {
  const method = req.method; // GET POST

  // 登录
  if (method === "POST" && req.path === "/api/user/login") {
    const { username, password } = req.body;
    const result = loginCheck(username, password);
    return result.then((data) => {
      if (data?.username) {
        return new SuccessModel("这是登录的接口");
      }

      return new ErrorModel("登录失败");
    });
  }
};

module.exports = handleUserRouter;

app.js

const qs = require("qs");

// 博客相关接口
const handleBlogRouter = require("./src/router/blog.js");
// 登录相关接口
const handleUserRouter = require("./src/router/user.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
      
    // 处理 user 路由
    // const userData = handleUserRouter(req, res);
    // if (userData) {
    //   res.end(JSON.stringify(userData));
    //   return;
    // }
    const userResult = handleUserRouter(req, res);
    if (userResult) {
      userResult.then((userData) => {
        res.end(JSON.stringify(userData));
      });

      return;
    }

    // 未命中路由,返回 404
    // 设置返回头状态码为 404,并设置返回的数据类型为 text/plain(纯文本)
    res.writeHead(404, { "Content-type": "text/plain" });
    res.write("404 Not Found\n");
    res.end();
  });
};

module.exports = serverHandle;

// process.env.NODE_ENV

总结

  • nodejs 链接 mysql,如何执行 sql 语句
  • 根据 NODE_ENV 区分配置
  • 封装 exec 函数,api 使用 exec 操作数据库

课程收获:
博客的登录,有了一点了解
图片描述

點(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
提交
取消