node博客小项目:接口开发、连接mysql数据库

本文涉及的产品
RDS MySQL DuckDB 分析主实例,集群系列 4核8GB
RDS MySQL DuckDB 分析主实例,基础系列 4核8GB
RDS AI 助手,专业版
简介: 【10月更文挑战第14天】node博客小项目:接口开发、连接mysql数据库

简介

本篇文章主要讲解使用NodeJS开发Web服务器(一个小型的博客项目)及NodeJS 如何连接 MySQL。

本文参考:NodeJS 连接 MySQL【NodeJS】_哔哩哔哩_bilibili

node服务器搭建

创建项目文件下,执行如下npm命令

npm init -y
npm i nodemon

创建如下文件夹及文件

image.png

入口文件 www.js

//引入http模块
const http = require('http')
const serverHandler = require('../app.js')
const PORT = 5000
//创建服务器
const server = http.createServer(serverHandler)
server.listen(PORT,() => {
   
    console.log('服务运行在5000端口...');
})

app.js

const serverHandler = (req,res) => {
   
    //设置返回的报文格式
    res.setHeader('Content-Type',"application/json")

    //返回内容
    res.end()
}

module.exports = serverHandler

配置文件package.json

{
   
  "name": "11",
  "version": "1.0.0",
  "description": "",
  "main": "bin/www.js",
  "scripts": {
   
    "test": "echo "Error: no test specified" && exit 1",
    "dev":"nodemon bin/www.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
   
    "nodemon": "^2.0.19"
  }
}

启动项目

npm run dev

路由初始化

创建博客处理模块 根目录 src\routes\blog.js

//处理博客相关路由
const handelBlogRoute = (req,res) => {
   
    //定义路由的处理逻辑
    const method = req.method

    if(method === "GET" && req.path === "/api/blog/list"){
   
        return {
   
            message:"获取博客接口列表"
        }
    }
    if(method === "GET" && req.path === "/api/blog/detail"){
   
        return {
   
            message:"获取博客详情接口"
        }
    }
    if(method === "POST" && req.path === "/api/blog/new"){
   
        return {
   "新建博客接口"
        }
    }
    if(method === "POST" && req.path === "/api/blog/update"){
   
        return {
   
            message:"更新博客接口"
        }
    }
    if(method === "POST" && req.path === "/srcapi/blog/delete"){
   
        return {
   
            message:"删除博客列表接口"
        }
    }
}

module.exports = handelBlogRoute

app.js中优化部分代码

const handelBlogRoute = require("./src/routes/blog")

const serverHandler = (req,res) => {
   
    //设置返回的报文格式
    res.setHeader('Content-Type',"application/json")
    const url = req.url
    req.path = url.split("?")[0]
    let blogData = handelBlogRoute(req,res)
    //返回内容
    if(blogData){
   
        res.end(JSON.stringify(blogData))
    }

    //匹配不到路由时的处理
    res.writeHead(404,{
   'Content-Type':'text/plain'})
    res.write("404 Not FOund")
    res.end()
}

module.exports = serverHandler

开发第一个路由

首先我们开发获取博客列表接口

1.创建src\contrllers\blog.js文件,用于设置返回接口数据

//博客相关方法
const getList = (author , keyword) => {
   
    //从数据库获取数据
    //先返回假数据
    return [
        {
    id:1, title:'标题1', content:"内容2",author:"张三", createAt:123456 },
        {
    id:2, title:'标题1', content:"内容2",author:"张三", createAt:1234567 },
        {
    id:3, title:'标题1', content:"内容2",author:"张三", createAt:12345678 },

    ]
}

module.exports = {
   
    getList
}

这个接口需要参数author , keyword关键词,用来返回匹配的数据。参数author , keyword是通过url的query参数获取的,因此需要解析url的query参数

2.解析url的query参数

app.js中

const handelBlogRoute = require("./src/routes/blog")
//node 原生的属性
const querystring = require("querystring")
//这里主要进行一些服务器的设置
const serverHandler = (req,res) => {
   
    //设置返回的报文格式
    res.setHeader('Content-Type',"application/json")

    //获取path    /api/blog/list
    const url = req.url
    req.path = url.split("?")[0]

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

    //路由匹配
    let blogData = handelBlogRoute(req,res)
    //返回内容
    if(blogData){
   
        res.end(JSON.stringify(blogData))
    }else{
   
        //匹配不到路由时的处理
        res.writeHead(404,{
   'Content-Type':'text/plain'})
        res.write("404 Not FOund")
        res.end()
    }  
}

module.exports = serverHandler

3.完善路由内容

src\routes\blog.js

const {
    SuccessModel } = require("../model/responseModel")
const {
    getList } = require("../contrllers/blog")
//处理博客相关路由
const handelBlogRoute = (req,res) => {
   
    //定义路由的处理逻辑
    const method = req.method

    if(method === "GET" && req.path === "/api/blog/list"){
   
        //可能的请求路径 api/blog/list?query=zhangsan&keyword=123

        const author = req.query.author || ""
        const keyword = req.query.keyword || ""
        //根据自定义的关键词,返回列表数据
        const listData = getList(author,keyword)
        //使用模型返回规范的文件格式
        return new SuccessModel(listData)
    }
    if(method === "GET" && req.path === "/api/blog/detail"){
   
        return {
   
            message:"获取博客详情接口"
        }
    }
    if(method === "POST" && req.path === "/api/blog/new"){
   
        return {
   
            message:"新建博客接口"
        }
    }
    if(method === "POST" && req.path === "/api/blog/update"){
   
        return {
   
            message:"更新博客接口"
        }
    }
    if(method === "POST" && req.path === "/srcapi/blog/delete"){
   
        return {
   
            message:"删除博客列表接口"
        }
    }
}

module.exports = handelBlogRoute

注意,为了使返回值更加规范方便,我们创建了一个类。

4.创建返回值模型

src\model\responseModel.js

class BaseModel {
   
    constructor(data,message) {
   
        if(typeof data == "string") {
   
            this.message = data;
            data = null
            message = null
        }
        if(data) {
   
            this.data = data
        }
        if(message) {
   
            this.message = message
        }
    }
}

//成功模型  实例化后 产生一个 {  data:data , errno : 0   }格式的对象
class SuccessModel extends BaseModel {
   
    constructor ( data ,message) {
   
        super(data,message)
        this.errno = 0
    }

}

//失败模型
class ErrorModel extends BaseModel {
   
    constructor ( data ,message) {
   
        super(data,message)
        this.errno = -1
    }

}

module.exports = {
   
     SuccessModel,
     ErrorModel

}

此时,访问接口,是可以获取数据的。

开发获取博客详情接口

逻辑如同获取博客列表

src\contrllers\blog.js

//博客相关方法
const getList = (author , keyword) => {
   
   .......
}

//获取博客详情数据
const getDetail = (id) => {
   
    //先返回假数据
    return  {
    id:1, title:'标题1', content:"内容2",author:"张三", createAt:123456 }
}

module.exports = {
   
    getList,
    getDetail
}

src\routes\blog.js

const {
    SuccessModel } = require("../model/responseModel")
const {
    getList ,getDetail} = require("../contrllers/blog")
//处理博客相关路由
const handelBlogRoute = (req,res) => {
   
    //定义路由的处理逻辑
    const method = req.method
    //获取接口列表    .....
    //获取接口详情
    if(method === "GET" && req.path === "/api/blog/detail"){
   
        const id = req.query.id
        const detailData =  getDetail(id)
        return new SuccessModel(detailData)
    }
    ......
}

module.exports = handelBlogRoute

访问接口

处理post请求

原生node的post请求是一个异步函数

我们需要在app.js中进行post请求处理

const handelBlogRoute = require("./src/routes/blog")
//node 原生的属性
const querystring = require("querystring")

//处理post数据
const getPostData = (req) => {
   
    const promise = new Promise((resolve,reject) => {
   
        if(req.method !== "POST"){
   
            resolve({
   })
            return 
        }
        if(req.headers['conten-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 serverHandler = (req,res) => {
   
    //设置返回的报文格式
    res.setHeader('Content-Type',"application/json")

    //获取path    /api/blog/list
    const url = req.url
    req.path = url.split("?")[0]

    //解析query
    req.query = querystring.parse(url.split("?")[1])
    //处理post数据
    getPostData(req).then((postData) => {
   
        //将post数据绑定在req的body上
        req.body = postData

        //路由匹配
        let blogData = handelBlogRoute(req,res)
        //返回内容
        if(blogData){
   
            res.end(JSON.stringify(blogData))
            return
        }
        //匹配不到路由时的处理
        res.writeHead(404,{
   'Content-Type':'text/plain'})
        res.write("404 Not FOund")
        res.end()
    })
}

module.exports = serverHandler

完善接口

src\routes\blog.js

const {
    SuccessModel,ErrorModel } = require("../model/responseModel")
const {
    getList ,getDetail,createNewBlog, deleteBlog,updataBlog} = require("../contrllers/blog")
//处理博客相关路由
const handelBlogRoute = (req,res) => {
   
    //定义路由的处理逻辑
    const method = req.method
    const id = req.query.id
    const postData = req.body
    //获取接口列表
    if(method === "GET" && req.path === "/api/blog/list"){
   
        //可能的请求路径 api/blog/list?query=zhangsan&keyword=123
        const author = req.query.author || ""
        const keyword = req.query.keyword || ""
        //根据自定义的关键词,返回列表数据
        const listData = getList(author,keyword)
        //使用模型返回规范的文件格式
        return new SuccessModel(listData)
    }
    //获取接口详情
    if(method === "GET" && req.path === "/api/blog/detail"){
   

        const detailData =  getDetail(id)
        return new SuccessModel(detailData)
    }
    //新建接口
    if(method === "POST" && req.path === "/api/blog/new"){
   

        const newBlogData = createNewBlog(postData)
        return new SuccessModel(newBlogData)
    }
    //更新博客路由
    if(method === "POST" && req.path === "/api/blog/update"){
   
        const updateBlogData = updataBlog(postData)
        if(updateBlogData){
   
            return new SuccessModel("更新博客成功")
        }else{
   
            return new ErrorModel("更新博客失败")
        }
    }
    //删除博客路由
    if(method === "POST" && req.path === "/srcapi/blog/delete"){
   
        const deleteBlogData = deleteBlog(id)
        if(deleteBlogData){
   
            return new SuccessModel("删除博客成功")
        }else{
   
            return new ErrorModel("删除博客失败")
        }
    }
}

module.exports = handelBlogRoute

src\contrllers\blog.js

//博客相关方法
const getList = (author , keyword) => {
   
    //从数据库获取数据
    //先返回假数据
    return [
        {
    id:1, title:'标题1', content:"内容2",author:"张三", createAt:123456 },
        {
    id:2, title:'标题1', content:"内容2",author:"张三", createAt:1234567 },
        {
    id:3, title:'标题1', content:"内容2",author:"张三", createAt:12345678 },
    ]
}

//获取博客详情数据
const getDetail = (id) => {
   
    //先返回假数据
    return  {
    id:1, title:'标题1', content:"内容2",author:"张三", createAt:123456 }
}

//创建新博客
const createNewBlog = (blogData) => {
    return {
    id:1 } }
//更新boke
const updataBlog = (id,blogData = {
   }) => {
     return true }
//删除博客
const deleteBlog = (id) => {
     return true }
module.exports = {
   
    getList,
    getDetail,
    createNewBlog,
    updataBlog,
    deleteBlog
}

使用mysql

1.创建数据库连接

image.png

2.创建myblog数据库,然后创建blogs数据表,表中按如图方式增加字段

image.png

数据表操作

image.png

基本的查找语句为:

SELECT * from blogs

查找时,也可以指定字段

select id,title from blogs

sql语句不区分大小写

注:语句前增加“--”可以注释语句

也可以进行筛选查询

SELECT * from blogs where title='标题1'
SELECT * from blogs where title='标题1' and author='gcshi'
SELECT * from blogs where title='标题1' or author='gcshi'
-- 模糊查询,查询标题包含1的内容
SELECT * from blogs WHERE title like '%1%'
-- 对查找的内容进行排序 默认为正序
SELECT * from blogs WHERE title like '%1%' order by id
-- 对查找的内容进行排序 倒序
SELECT * from blogs WHERE title like '%1%' order by id desc

insert into blogs(title,content,author,createdAt) value ('标题1','内容1','gcshi',1234567890123)

我们可以选择我们的增加语句,然后点击【运行已选择的】

image.png

update blogs set title='11111'

这种方法会将所有数据的标题修改成111

image.png

我们可以增加筛选条件

update blogs set title='222' where content='内容1'

-- 这会删除整张表
delete from blogs

上述命令会删除整张表,这是危险操作。我们应该加上条件

delete from blogs WHERE title='222'

node连接数据库

npm i mysql
├─ mysql-demo
│  └─ index.js
//引入mysql
const mysql = require('mysql')

// 创建连接对象
const connection =  mysql.createConnection({
   
  host:'localhost',
  user:'root',
  password:'root',
  port:3306,
  database:'myblog'
})

//开始连接
connection.connect();

//执行sql语句
const sql = 'select * from blogs'

connection.query(sql,(err,result) => {
   
  if(err){
   
    return console.log(err);
  }
  console.log('reult',result);
})

//关闭连接
connection.end()

我们在mysql-demo文件夹中运行一下 node index.js,可以看到返回的数据结果(一个json数据格式)
image.png

sql语句封装

全局调用sql语句会很乱,我们将sql语句进行封装,便于我们在项目中使用。

src下创建db文件夹,创建mysql.js文件

└─ src
     ├─ config               //数据库配置文件夹
   │  └─ db.js
   ├─ contrllers
   │  └─ blog.js
   ├─ db
   │  └─ mysql.js             
   ├─ model
   │  └─ responseModel.js
   └─ routes
      └─ blog.js
//引入mysql
const mysql = require('mysql')

const {
    MYSQL_CONFIG } = require('../config/db')

// 创建连接对象
const connection =  mysql.createConnection( MYSQL_CONFIG )

//开始连接
connection.connect();

//执行sql语句,封装成一个promise函数
function execSQL(sql) {
   
    const promise = new Promise((resolve, reject) => {
   
        connection.query(sql,(err,result)=>{
   
            if(err){
   
                return reject(err)
            }
            resolve(result)
        })
    })
    return promise
}

module.exports = {
   
    execSQL
}

为了方便数据库不同环境下的配置的修改,我们将其单独配置src\config\db.js

let MYSQL_CONFIG = {
   }

MYSQL_CONFIG = {
   
    host:'localhost',
    user:'root',
    password:'root',
    port:3306,
    database:'myblog'
}

module.exports = {
   
    MYSQL_CONFIG
}

获取博客列表对接Mysql

src\contrllers\blog.js

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

//获取博客列表
const getList = (author , keyword) => {
   
   let sql = "select * from blogs where 1=1 "
   if( author ){
   
       sql += `and author=${
     author} `
   }
   if( keyword ){
   
       sql += `and title like '%${
     keyword}%' `
   }
   return execSQL(sql) 
}

//获取博客详情数据

//创建新博客

//更新boke

//删除博客

module.exports = {
   
    getList,
    getDetail,
    createNewBlog,
    updataBlog,
    deleteBlog
}

src\routes\blog.js

const {
    SuccessModel,ErrorModel } = require("../model/responseModel")
const {
    getList ,getDetail,createNewBlog, deleteBlog,updataBlog} = require("../contrllers/blog")
//处理博客相关路由
const handelBlogRoute = (req,res) => {
   
    //定义路由的处理逻辑
    const method = req.method
    const id = req.query.id
    const postData = req.body
    //获取接口列表
    if(method === "GET" && req.path === "/api/blog/list"){
   
        //可能的请求路径 api/blog/list?query=zhangsan&keyword=123
        const author = req.query.author || ""
        const keyword = req.query.keyword || ""
        //根据自定义的关键词,返回列表数据
        return getList(author,keyword).then(res => {
   
            //使用模型返回规范的文件格式
            return new SuccessModel(res)
        })
    }
    //获取接口详情

    //新建接口

    //更新博客路由

    //删除博客路由
}

module.exports = handelBlogRoute

app.js

const handelBlogRoute = require("./src/routes/blog")
//node 原生的属性
const querystring = require("querystring")

//处理post数据
const getPostData = (req) => {
   
    const promise = new Promise((resolve,reject) => {
   
        if(req.method !== "POST"){
   
            resolve({
   })
            return 
        }
        if(req.headers['conten-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)
            )
        })
    })
    return promise
}

//这里主要进行一些服务器的设置
const serverHandler = (req,res) => {
   
    //设置返回的报文格式
    res.setHeader('Content-Type',"application/json")

    //获取path    /api/blog/list
    const url = req.url
    req.path = url.split("?")[0]

    //解析query
    req.query = querystring.parse(url.split("?")[1])
    //处理post数据
    getPostData(req).then((postData) => {
   
        //将post数据绑定在req的body上
        req.body = postData

        //路由匹配
        let promiseBlogData = handelBlogRoute(req,res)
        //返回内容
        if(promiseBlogData){
   
            promiseBlogData.then(promiseBlogData =>{
   
                res.end(JSON.stringify(promiseBlogData))
            })
            return
        }
        //匹配不到路由时的处理
        res.writeHead(404,{
   'Content-Type':'text/plain'})
        res.write("404 Not FOund")
        res.end()
    })
}

module.exports = serverHandler

博客详情、新建博客对接Mysql

src\contrllers\blog.js新建博客

//创建新博客
const createNewBlog = (blogData = {
   }) => {
   
    const title = blogData.title
    const content = blogData.content
    const author = blogData.author
    const createdAt = Date.now()
    const sql = `
        insert into blogs (title, content ,author ,createdAt) values ('${
     title}', '${
     content}', '${
     author}', ${
     createdAt});
    `
    return execSQL(sql).then(res => {
   
        console.log(res);
    })
}

插入成功后的返回结果

因此,我们的更新博客和获取博客详情接口如下

src\contrllers\blog.js


//获取博客详情数据 通过id获取内容
const getDetail = (id) => {
   
    let sql = `select * from blogs where id=${
     id} `
    return execSQL(sql)
}

//创建新博客
const createNewBlog = (blogData = {
   }) => {
   
    const title = blogData.title
    const content = blogData.content
    const author = blogData.author
    const createdAt = Date.now()
    const sql = `
        insert into blogs (title, content ,author ,createdAt) values ('${
     title}', '${
     content}', '${
     author}', ${
     createdAt});
    `
    return execSQL(sql).then(res => {
   
        return {
    id: res.insertId}
    })
}

src\routes\blog.js

    //获取接口详情 g根据id获取内容
    if(method === "GET" && req.path === "/api/blog/detail"){
   
        return getDetail(id).then( res => {
   
            return new SuccessModel(res)
        })
    }
    //新建接口
    if(method === "POST" && req.path === "/api/blog/new"){
   
        return createNewBlog(postData).then(res => {
   
            return new SuccessModel(res)
        })
    }
相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
MySQL数据库入门学习
本课程通过最流行的开源数据库MySQL带你了解数据库的世界。   相关的阿里云产品:云数据库RDS MySQL 版 阿里云关系型数据库RDS(Relational Database Service)是一种稳定可靠、可弹性伸缩的在线数据库服务,提供容灾、备份、恢复、迁移等方面的全套解决方案,彻底解决数据库运维的烦恼。 了解产品详情: https://www.aliyun.com/product/rds/mysql 
相关文章
|
9月前
|
Java 关系型数据库 MySQL
在Linux平台上进行JDK、Tomcat、MySQL的安装并部署后端项目
现在,你可以通过访问http://Your_IP:Tomcat_Port/Your_Project访问你的项目了。如果一切顺利,你将看到那绚烂的胜利之光照耀在你的项目之上!
480 41
|
人工智能 JavaScript 关系型数据库
【02】Java+若依+vue.js技术栈实现钱包积分管理系统项目-商业级电玩城积分系统商业项目实战-ui设计图figmaUI设计准备-figma汉化插件-mysql数据库设计-优雅草卓伊凡商业项目实战
【02】Java+若依+vue.js技术栈实现钱包积分管理系统项目-商业级电玩城积分系统商业项目实战-ui设计图figmaUI设计准备-figma汉化插件-mysql数据库设计-优雅草卓伊凡商业项目实战
467 14
【02】Java+若依+vue.js技术栈实现钱包积分管理系统项目-商业级电玩城积分系统商业项目实战-ui设计图figmaUI设计准备-figma汉化插件-mysql数据库设计-优雅草卓伊凡商业项目实战
|
11月前
|
PHP 数据库
【YashanDB知识库】PHP使用OCI接口使用数据库绑定参数功能异常
【YashanDB知识库】PHP使用OCI接口使用数据库绑定参数功能异常
|
NoSQL Java 关系型数据库
Liunx部署java项目Tomcat、Redis、Mysql教程
本文详细介绍了如何在 Linux 服务器上安装和配置 Tomcat、MySQL 和 Redis,并部署 Java 项目。通过这些步骤,您可以搭建一个高效稳定的 Java 应用运行环境。希望本文能为您在实际操作中提供有价值的参考。
862 26
|
JavaScript 安全 Java
java版药品不良反应智能监测系统源码,采用SpringBoot、Vue、MySQL技术开发
基于B/S架构,采用Java、SpringBoot、Vue、MySQL等技术自主研发的ADR智能监测系统,适用于三甲医院,支持二次开发。该系统能自动监测全院患者药物不良反应,通过移动端和PC端实时反馈,提升用药安全。系统涵盖规则管理、监测报告、系统管理三大模块,确保精准、高效地处理ADR事件。
568 1
|
分布式计算 关系型数据库 MySQL
SpringBoot项目中mysql字段映射使用JSONObject和JSONArray类型
SpringBoot项目中mysql字段映射使用JSONObject和JSONArray类型 图像处理 光通信 分布式计算 算法语言 信息技术 计算机应用
309 8
|
SQL Java 数据库连接
数据库常用接口
ODBC(Open Database Connectivity):开放数据库互连技术为访问不同的SQL数据库提供了一个共同的接口。ODBC使用SQL作为访问数据的标准。这一接口提供了最大限度的互操作性,一个应用程序可以通过共同的一组代码访问不同的SQL数据库管理系统(DBMS)。 一个基于ODBC的应用程序对数据库的操作不依赖任何DBMS,不直接与DBMS打交道,所有的数据库操作由对应的DBMS的ODBC驱动程序完成。也就是说,不论是Access,MySQL还是Oracle数据库,均可用ODBC API进行访问。由此可见,ODBC的最大优点是能以统一的方式处理所有的数据库。
|
监控 前端开发 Java
【技术开发】接口管理平台要用什么技术栈?推荐:Java+Vue3+Docker+MySQL
该文档介绍了基于Java后端和Vue3前端构建的管理系统的技术栈及功能模块,涵盖管理后台的访问、登录、首页概览、API接口管理、接口权限设置、接口监控、计费管理、账号管理、应用管理、数据库配置、站点配置及管理员个人设置等内容,并提供了访问地址及操作指南。
|
7月前
|
JavaScript Unix Linux
nvm与node.js的安装指南
通过以上步骤,你可以在各种操作系统上成功安装NVM和Node.js,从而在不同的项目中灵活切换Node.js版本。这种灵活性对于管理不同项目的环境依赖而言是非常重要的。
1892 11

推荐镜像

更多