Node.js 从零开发 web server博客项目[数据存储]
web server博客项目
- Node.js 从零开发 web server博客项目[项目介绍]
- Node.js 从零开发 web server博客项目[接口]
- Node.js 从零开发 web server博客项目[数据存储]
- Node.js 从零开发 web server博客项目[登录]
- Node.js 从零开发 web server博客项目[日志]
- Node.js 从零开发 web server博客项目[安全]
- Node.js 从零开发 web server博客项目[express重构博客项目]
- Node.js 从零开发 web server博客项目[koa2重构博客项目]
- Node.js 从零开发 web server博客项目[上线与配置]
文章目录
nodejs链接 mysql 封装成工具
安装
MySQL
cnpm i mysql -S
创建
src/conf/db.js
const env = process.env.NODE_ENV // 环境参数
// 配置
let MYSQL_CONF
if (env === 'dev') {
MYSQL_CONF = {
host: 'localhost',
user: 'root',
password: 'root',
port: '3306',
database: 'myblog'
}
}
if (env === 'production') {
MYSQL_CONF = {
host: 'localhost',
user: 'root',
password: 'root',
port: '3306',
database: 'myblog'
}
}
module.exports = { MYSQL_CONF }
- 创建
scr/db/mysql.js
const mysql = require('mysql')
const { MYSQL_CONF } = require('../conf/db')
// 创建链接对象
var con = mysql.createConnection(MYSQL_CONF);
// 开始链接
con.connect();
// 统一执行 sql 的函数
function exec(sql) {
const promise = new Promise((resolve, reject) => {
con.query(sql, function (error, result) {
if (error) {
reject(error)
return
}
resolve(result)
})
})
return promise
}
module.exports = {
exec
}
API对接MySQL (博客列表)
controller/blog.js
// 博客列表
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}%' `
}
sql += `order by createtime desc;`
return exec(sql)
// [{
// id: 1,
// title: '标题a',
// content: '内容a',
// createTime: 1562085127324,
// suthor: 'zhangsan'
// }]
}
router/blog.js
// 获取博客列表
if (method === 'GET' && path === '/api/blog/list') {
const {
author,
keyword
} = req.query || ''
// const listData = getList(author, keyword)
// return new SuccessModel(listData)
const result = getList(author, keyword)
return result.then(listData => {
return new SuccessModel(listData)
})
}
app.js
getPostData(req).then(postData => {
req.body = postData
// 处理 blog 路由
// const blogData = handleBlogRouter(req, res)
// if (blogData) {
// res.end(
// JSON.stringify(blogData)
// )
// return
// }
const blogResult = handleBlogRouter(req, res)
if (blogResult) {
blogResult.then(blogData => {
res.end(
JSON.stringify(blogData)
)
})
return
}
...
// 未命中路由, 返回404
res.writeHead(404, {
"content-type": "text/plain"
})
res.write("404 Not Found\n")
res.end()
})
API对接MySQL (博客详情和新建)
controller/blog.js
const { exec } = require('../db/mysql')
// 博客内容
const getDtail = (id) => {
// return {
// id: 1,
// title: '标题a',
// content: '内容a',
// createTime: 1562085127324,
// suthor: 'zhangsan'
// }
let sql = `select * from blogs where id='${id}'`
return exec(sql).then(rows => {
return rows[0]
})
}
// 新增一篇博客
const newBlog = (blogData) => {
// 赋予id
// return {
// id: 3
// }
const {title, content, author} = blogData
const createtime = Date.now()
let sql = `insert into blogs (title, content, createtime, author) values ('${title}', '${content}', '${createtime}', '${author}');`
return exec(sql)
}
router/blog.js
// 获取一篇博客的内容
if (method === 'GET' && path === '/api/blog/detail') {
// const data = getDtail(id)
// return new SuccessModel(data)
const result = getDtail(id)
return result.then(data => {
return new SuccessModel(data)
})
}
// 新增一篇博客
if (method === 'POST' && 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)
})
}
API对接MySQL (更新和删除)
更新
// 更新一篇博客
const updateBlog = (id, blogData = {}) => {
// console.log(`更新一篇博客, ID:${id}, 内容:${blogData}`)
// return true
const {title, content} = blogData
const sql = `update blogs set title='${title}', content='${content}' where id=${id}`
return exec(sql).then(updateData => {
console.log('updateData is ', updateData);
if (updateData.affectedRows > 0) {
return true
}
return false
})
}
*********************
// 更新一篇博客
if (method === 'POST' && path === '/api/blog/update') {
const result = updateBlog(id, req.body)
// if (result) {
// return new SuccessModel(data)
// } else {
// return ErrorModel('更新博客失败')
// }
return result.then(val => {
if (val) {
return new SuccessModel()
} else {
return ErrorModel('更新博客失败')
}
})
}
删除
// 删除一篇博客
const delBlog = (id, author) => {
// console.log(`删除一篇博客, ID:${id}`)
// return true
const sql = `delete from blogs where id='${id}' and author='${author}'`
return exec(sql).then(delData => {
if (delData.affectedRows > 0) {
return true
}
return false
})
}
*********************
// 删除一篇博客
if (method === 'POST' && path === '/api/blog/del') {
// const result = delBlog(id)
// if (result) {
// return new SuccessModel(result)
// } else {
// return new ErrorModel('删除博客失败')
// }
const author = 'zhangsan'
const result = delBlog(id, author)
return result.then(val => {
if (val) {
return new SuccessModel(result)
} else {
return new ErrorModel('删除博客失败')
}
})
}
API对接MySQL (登录)
controller/user.js
const { exec } = require('../db/mysql')
const loginCheck = (username, password) => {
// if (username === 'zhangsan' && password === '1234') {
// return true
// }
const sql = `select username, realname from users where username='${username}' and password='${password}'`
return exec(sql).then(rows => {
return rows[0] || {}
})
}
module.exports = {
loginCheck
}
router/user.js
const {
loginCheck
} = require('../controller/user')
const { SuccessModel, ErrorModel } = require('../model/resModel')
const handleUserRouter = (req, res) => {
const {
method,
path
} = req
// 登录
if (method === 'POST' && path === '/api/user/login') {
const {
username,
password
} = req.body
const result = loginCheck(username, password)
// if (result) {
// return new SuccessModel(result)
// } else {
// return new ErrorModel('登录失败')
// }
return result.then(data => {
if (data.username) {
return new SuccessModel()
}
return new ErrorModel('登录失败')
})
}
}
module.exports = handleUserRouter
app.js
// 处理 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
}
```
Node.js 从零开发 web server博客项目[数据存储]的更多相关文章
- Node.js 从零开发 web server博客项目[express重构博客项目]
web server博客项目 Node.js 从零开发 web server博客项目[项目介绍] Node.js 从零开发 web server博客项目[接口] Node.js 从零开发 web se ...
- Node.js 从零开发 web server博客项目[koa2重构博客项目]
web server博客项目 Node.js 从零开发 web server博客项目[项目介绍] Node.js 从零开发 web server博客项目[接口] Node.js 从零开发 web se ...
- Node.js 从零开发 web server博客项目[安全]
web server博客项目 Node.js 从零开发 web server博客项目[项目介绍] Node.js 从零开发 web server博客项目[接口] Node.js 从零开发 web se ...
- Node.js 从零开发 web server博客项目[日志]
web server博客项目 Node.js 从零开发 web server博客项目[项目介绍] Node.js 从零开发 web server博客项目[接口] Node.js 从零开发 web se ...
- Node.js 从零开发 web server博客项目[登录]
web server博客项目 Node.js 从零开发 web server博客项目[项目介绍] Node.js 从零开发 web server博客项目[接口] Node.js 从零开发 web se ...
- Node.js 从零开发 web server博客项目[接口]
web server博客项目 Node.js 从零开发 web server博客项目[项目介绍] Node.js 从零开发 web server博客项目[接口] Node.js 从零开发 web se ...
- Node.js 从零开发 web server博客项目[项目介绍]
web server博客项目 Node.js 从零开发 web server博客项目[项目介绍] Node.js 从零开发 web server博客项目[接口] Node.js 从零开发 web se ...
- Vue+node.js实现一个简洁的个人博客系统
本项目是一个用vue和node以及mysql实现的一个简单的个人博客系统,整体逻辑比较简单.但是可以我们完整的了解一个项目从数据库到后端到前端的实现过程,适合不太懂这一块的朋友们拿来练手. 本项目所用 ...
- github pages + Hexo + node.js 搭建属于自己的个人博客网站
之前我写过一篇用Github实现个人主页的博客:https://www.cnblogs.com/tu-0718/p/8081288.html 后来看到某个大佬写的文章:[5分钟 0元搭建个人独立 ...
随机推荐
- 【算法•日更•第三十七期】A*寻路算法
▎写在前面 这是一种搜索算法,小编以前总是念成A乘寻路算法,没想到一直念错. 请大家都念成A星寻路算法,不要像小编一样丢人了. ▎A*寻路算法 ☞『引入』 相信大家都或多或少的玩过一些游戏吧,那么游戏 ...
- 《Java从入门到失业》第二章:Java环境(二):JDK、JRE、JVM
2.2JDK.JRE.JVM 在JDK的安装目录中,我们发现有一个目录jre(其实如果是下一步下一步安装的,在和JDK安装目录同级目录下,还会有一个jre目录).初学Java的同学,有时候搞不清楚这3 ...
- Istio 网络弹性 实践 之 故障注入 和 调用超时
网络弹性介绍 网络弹性也称为运维弹性,是指网络在遇到灾难事件时快速恢复和继续运行的能力.灾难事件的范畴很广泛,比如长时间停电.网络设备故障.恶意入侵等. 超时时间 工作中常常会碰到这样的开发.测试场景 ...
- python3.6和pip3:Ubuntu下安装升级与踩坑之路
本文以Ubuntu16.x系统为例,演示如何安装python3.6和相应环境.安装Python3的机器必须要能访问外网才能进行如下操作! 1. 安装方式 在Ubuntu下安装python有两种方式: ...
- 区块链入门到实战(31)之Solidity – 第一个程序
为简单起见,我们使用在线Solidity开发工具Remix IDE编译和运行Solidity程序. 第1步 – 在File explorers选项卡下,新建一个test1.sol文件,代码如下: 示例 ...
- 安装oracleXE快捷版(一)
yum找不到包,参考了一些文章,用iso上的包安装了.在文章后面贴有我实际的操作(黑体)和日志. 更换yum源https://www.cnblogs.com/zrxuexi/p/11587173.ht ...
- 如何成为一位合格的ScrumMaster
嗨,大家好,我是叶子 ScrumMaster的职责简单理解为:确保团队按照scrum的方式运行,团队的教练,帮助团队更好的工作,过程中的执行者,能够在team和po之间平衡.移除项目进度的障碍,保护团 ...
- Spark RDD中Runtime流程解析
一.Runtime架构图 (1)从Spark Runtime的角度讲,包括五大核心对象:Master.Worker.Executor.Driver.CoarseGrainedExecutorBack ...
- Jira + confluence
Jira入门教程 敏捷开发管理(一) https://www.jianshu.com/p/145b5c33f7d0 https://www.jianshu.com/p/975385878cde JIR ...
- webdriver入门之环境准备
1.安装ruby 下载ruby的安装包,很简单,不解释.装好之后打开cmd输入以下命令验证是否安装成功 ruby -v 2.安装webdriver 确保机器联网,用gem命令安装是在有网络的情况下进行 ...