web server博客项目

  1. Node.js 从零开发 web server博客项目[项目介绍]
  2. Node.js 从零开发 web server博客项目[接口]
  3. Node.js 从零开发 web server博客项目[数据存储]
  4. Node.js 从零开发 web server博客项目[登录]
  5. Node.js 从零开发 web server博客项目[日志]
  6. Node.js 从零开发 web server博客项目[安全]
  7. Node.js 从零开发 web server博客项目[express重构博客项目]
  8. Node.js 从零开发 web server博客项目[koa2重构博客项目]
  9. 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博客项目[数据存储]的更多相关文章

  1. Node.js 从零开发 web server博客项目[express重构博客项目]

    web server博客项目 Node.js 从零开发 web server博客项目[项目介绍] Node.js 从零开发 web server博客项目[接口] Node.js 从零开发 web se ...

  2. Node.js 从零开发 web server博客项目[koa2重构博客项目]

    web server博客项目 Node.js 从零开发 web server博客项目[项目介绍] Node.js 从零开发 web server博客项目[接口] Node.js 从零开发 web se ...

  3. Node.js 从零开发 web server博客项目[安全]

    web server博客项目 Node.js 从零开发 web server博客项目[项目介绍] Node.js 从零开发 web server博客项目[接口] Node.js 从零开发 web se ...

  4. Node.js 从零开发 web server博客项目[日志]

    web server博客项目 Node.js 从零开发 web server博客项目[项目介绍] Node.js 从零开发 web server博客项目[接口] Node.js 从零开发 web se ...

  5. Node.js 从零开发 web server博客项目[登录]

    web server博客项目 Node.js 从零开发 web server博客项目[项目介绍] Node.js 从零开发 web server博客项目[接口] Node.js 从零开发 web se ...

  6. Node.js 从零开发 web server博客项目[接口]

    web server博客项目 Node.js 从零开发 web server博客项目[项目介绍] Node.js 从零开发 web server博客项目[接口] Node.js 从零开发 web se ...

  7. Node.js 从零开发 web server博客项目[项目介绍]

    web server博客项目 Node.js 从零开发 web server博客项目[项目介绍] Node.js 从零开发 web server博客项目[接口] Node.js 从零开发 web se ...

  8. Vue+node.js实现一个简洁的个人博客系统

    本项目是一个用vue和node以及mysql实现的一个简单的个人博客系统,整体逻辑比较简单.但是可以我们完整的了解一个项目从数据库到后端到前端的实现过程,适合不太懂这一块的朋友们拿来练手. 本项目所用 ...

  9. github pages + Hexo + node.js 搭建属于自己的个人博客网站

     之前我写过一篇用Github实现个人主页的博客:https://www.cnblogs.com/tu-0718/p/8081288.html   后来看到某个大佬写的文章:[5分钟 0元搭建个人独立 ...

随机推荐

  1. 设置与查看Linux系统中的环境变量

    大家好,我是良许. 大家都知道,在 Linux 系统中,有环境变量和 Shell 变量这两种变量. 环境变量是在程序及其子程序中全局可用的,常常用来储存像默认的文本编辑器或者浏览器,以及可执行文件的路 ...

  2. Collections.synchronizedMap()与ConcurrentHashMap区别

    Collections.synchronizedMap()与ConcurrentHashMap主要区别是:Collections.synchronizedMap()和Hashtable一样,实现上在调 ...

  3. 逃离CSDN -慕舲的黑夜-第三期

    来时,是朋友推荐查资料,后来看到CSDN的UI,好华丽高大上,也读了CSDN首页推荐的一些文章,加入CSDN. 可是后来随着博客园,蓝奏云,w3c菜鸟教程,等平台的出现,CSDN越来越令人心寒

  4. linux驱动之内核空间几种长延时的实现策略的优劣评估

    本文转载自http://blog.chinaunix.net/uid-23769728-id-3084737.html 这里所谓的长延时,是指其实现时间延时的粒度可以在HZ这一水准上.<深入Li ...

  5. SVG的引入历程

    直接引入编辑器会报错 Google: typescript svg cannot find module找到 这个网址 我放到了 shims-vue.d.ts 里面 declare module &q ...

  6. 2019年达内云PS淘宝美工平面UI/UX/UE/UED影视后期交互设计师视频

    2019年达内云PS淘宝美工平面UI/UX/UE/UED影视后期交互设计师视频 百度网盘链接一 百度网盘链接二

  7. Java面试题(网络篇)

    网络 79.http 响应码 301 和 302 代表的是什么?有什么区别? 301,302 都是HTTP状态的编码,都代表着某个URL发生了转移. 区别: 301 redirect: 301 代表永 ...

  8. 赫然:怎样学习seo优化技术

    http://www.wocaoseo.com/thread-79-1-1.html 今天的题目是学习SEO起步阶段每个人都要问的.SEO怎么学?如何进阶SEO技能?都包括哪些知识?笔者也自己总结过一 ...

  9. Python爬取网易云音乐歌手歌曲和歌单

    仅供学习参考 Python爬取网易云音乐网易云音乐歌手歌曲和歌单,并下载到本地 很多人学习python,不知道从何学起.很多人学习python,掌握了基本语法过后,不知道在哪里寻找案例上手.很多已经做 ...

  10. 分布式文件存储:FastDFS简单使用与原理分析

    引言 FastDFS 属于分布式存储范畴,分布式文件系统 FastDFS 非常适合中小型项目,在我接手维护公司图片服务的时候开始接触到它,本篇文章目的是总结一下 FastDFS 的知识点. 用了 2 ...