01——开发博客项目之接口06

    科技2026-06-18  17

    1.更新博客接口 2.删除博客接口 3.登陆接口

    1.更新博客接口

    router/blog.js

    // 更新一篇博客 if (method === 'POST' && req.path ==='/api/blog/update') { const result = updateBlog(id, req.body) if (result) { return new SuccessModel() } else { return new ErrorModel('更新博客失败') } }

    controller/blog.js

    // 更新博客处理逻辑 const updateBlog = (id, blogData = {}) => { console.log('update blog',id, blogData) return true }

    2.删除博客接口

    router/blog.js

    // 删除一篇博客 if (method === 'POST' && req.path ==='/api/blog/delete') { const result = delBlog(id) if (result) { return new SuccessModel() } else { return new ErrorModel('删除博客失败') } }

    controller/blog.js

    // 删除博客处理逻辑 const delBlog = (id) => { return true }

    3.登陆接口

    router/user.js const { loginCheck} = require('../controller/user') const { SuccessModel, ErrorModel} = require('../model/resModel') const handleUserBlog = (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) console.log(result) if (result) { return new SuccessModel() } return new ErrorModel('登陆失败') } } module.exports = handleUserBlog

    controller/user.js

    const loginCheck = (username, password) => { // 先使用假数据 if (username === 'zhangsan' && password === '123456') { return true } return false } module.exports = { loginCheck }
    Processed: 0.014, SQL: 9