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博客项目[上线与配置]
文章目录
目录结构
|-- app.js
|-- package.json
|-- bin
| |-- www.js
|-- src
|-- controller
| |-- blog.js
| |-- user.js
|-- model
| |-- resModel.js
|-- router
|-- blog.js
|-- user.js
package.json
{
"name": "blog-1",
"version": "1.0.0",
"description": "",
"main": "bin/www.js",
"scripts": {
"dev": "cross-env NODE_ENV=dev nodemon ./bin/www.js",
"prd": "cross-env NODE_ENV=production nodemon ./bin/www.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"cross-env": "^5.2.0",
"nodemon": "^1.19.1"
}
}
www.js
const http = require('http')
const port = 9527
const serverHandle = require('../app')
const server = http.createServer(serverHandle)
server.listen(port, () => {
console.log(`server is running on ${port}`)
})
app.js
const querystring = require('querystring')
const handleBlogRouter = require('./src/router/blog')
const handleUserRouter = require('./src/router/user')
// 处理 post data
const getPostData = (req) => {
const promise = 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) {
resovle({})
return
}
resolve(
JSON.parse(postData)
)
})
})
return promise
}
const serverHandle = (req, res) => {
// 设置返回文件格式 JSON
res.setHeader('Content-type', 'application/json')
const url = req.url
// 获取path
req.path = url.split('?')[0]
// 解析 query
req.query = querystring.parse(url.split('?')[1])
getPostData(req).then(postData => {
req.body = postData
// 处理 blog 路由
const blogData = handleBlogRouter(req, res)
if (blogData) {
res.end(
JSON.stringify(blogData)
)
return
}
// 处理 user 路由
const userData = handleUserRouter(req, res)
if (userData) {
res.end(
JSON.stringify(userData)
)
return
}
// 未命中路由, 返回404
res.writeHead(404, {
"content-type": "text/plain"
})
res.write("404 Not Found\n")
res.end()
})
}
module.exports = serverHandle
// process.env.NODE_ENV
router/blog.js
const {
getList,
getDtail,
newBlog,
updateBlog,
delBlog
} = require('../controller/blog')
const {
SuccessModel,
ErrorModel
} = require('../model/resModel')
const handleBlogRouter = (req, res) => {
const {
method,
path
} = req
const id = req.query.id
// 获取博客列表
if (method === 'GET' && path === '/api/blog/list') {
const {
author,
keyword
} = req.query || ''
const listData = getList(author, keyword)
return new SuccessModel(listData)
}
// 获取一篇博客的内容
if (method === 'GET' && path === '/api/blog/detail') {
const data = getDtail(id)
return new SuccessModel(data)
}
// 新增一篇博客
if (method === 'POST' && path === '/api/blog/new') {
const data = newBlog(req.body)
return new SuccessModel(data)
}
// 更新一篇博客
if (method === 'POST' && path === '/api/blog/update') {
const result = updateBlog(id, req.body)
if (result) {
return new SuccessModel(data)
} else {
return ErrorModel('更新博客失败')
}
}
// 删除一篇博客
if (method === 'POST' && path === '/api/blog/del') {
const result = delBlog(id)
if (result) {
return new SuccessModel(result)
} else {
return new ErrorModel('删除博客失败')
}
}
}
module.exports = handleBlogRouter
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('登录失败')
}
}
}
module.exports = handleUserRouter
假数据
controller/blog.js
const getList = (author, keyword) => {
// 博客列表
return [{
id: 1,
title: '标题a',
content: '内容a',
createTime: 1562085127324,
suthor: 'zhangsan'
},
{
id: 2,
title: '标题b',
content: '内容b',
createTime: 1562085168425,
suthor: 'lisi'
},
]
}
// 博客内容
const getDtail = (id) => {
return {
id: 1,
title: '标题a',
content: '内容a',
createTime: 1562085127324,
suthor: 'zhangsan'
}
}
// 新增一篇博客
const newBlog = (blogData) => {
// 赋予id
return {
id: 3
}
}
// 更新一篇博客
const updateBlog = (id, blogData = {}) => {
console.log(`更新一篇博客, ID:${id}, 内容:${blogData}`)
return true
}
// 删除一篇博客
const delBlog = (id) => {
console.log(`删除一篇博客, ID:${id}`)
return true
}
module.exports = {
getList,
getDtail,
newBlog,
updateBlog,
delBlog
}
controller/user.js
const loginCheck = (username, password) => {
if (username === 'zhangsan' && password === '1234') {
return true
}
}
module.exports = {
loginCheck
}
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博客项目[数据存储]
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 ...
- Vue+node.js实现一个简洁的个人博客系统
本项目是一个用vue和node以及mysql实现的一个简单的个人博客系统,整体逻辑比较简单.但是可以我们完整的了解一个项目从数据库到后端到前端的实现过程,适合不太懂这一块的朋友们拿来练手. 本项目所用 ...
- Node.js调用百度地图Web服务API的Geocoding接口进行点位反地理信息编码
(从我的新浪博客上搬来的,做了一些修改.) 最近迷上了node.js以及JavaScript.现在接到一个活,要解析一个出租车点位数据的地理信息.于是就想到使用Node.js调用百度地图API进行解析 ...
随机推荐
- 《Java从入门到失业》第二章:Java环境(一):Java SE安装
从这一章开始,终于我们可以开始正式进入Java世界了.前面我们提到过,Java分三个版本,我们这里只讨论Java SE. 2.1Java SE安装 所谓工欲善其事,必先利其器.第一步,我们当然是要下载 ...
- 【译】New experimental Razor editor for Visual Studio
随着 Visual Studio 2019 16.7 Preview 4 的发布,现在可以尝试我们新的实验性 Razor 编辑器,用于本地开发,包括 MVC.Razor Page 和 Blazor.我 ...
- Linux C++向josn文件写入字符串
对C++了解的不多,我的需求是往JSON文件里写入字符串,这种方式不需要用开源的库,直接写文件就可以.附上代码需要包含studio.h头文件以计fstream long a=123456; char ...
- Scala中的Map集合
1. Map集合 1.1 Scala中的Map介绍 Scala中的Map 和Java类似,也是一个散列表,它存储的内容也是键值对(key-value)映射,Scala中不可变的Map是有序的,可变的M ...
- linux线程控制-2(线程控制函数)
记录肖堃老师讲解的linux线程 1. 创建线程 int pthread_create( (pthread_t *thread, pthread_attr_t *attr, void *(*start ...
- Jmeter 常用函数(10)- 详解 __threadNum
如果你想查看更多 Jmeter 常用函数可以在这篇文章找找哦 https://www.cnblogs.com/poloyy/p/13291704.html 作用 返回当前线程组产生的线程的线程编号 语 ...
- JavaFX桌面应用-loading界面
上次使用JavaFX开发了一个视频转码工具,当用户点击"启动"按钮开始转码的时候,会禁用启动按钮,防止多次启动转码. 这种处理方式对用户来说可能并是很友好,其实可以在启动转码的时弹 ...
- 多主机搭建etcd集群
下载https://github.com/etcd-io/etcd/releases/download/v3.4.10/etcd-v3.4.10-linux-amd64.tar.gz分别放到两台主机上 ...
- openstack nova 手动修改虚拟机状态
source admin-openrc.sh nova list nova reset-state <id> --active
- COS418, Distributed System, Go Language
本博客是MIT的分布式系统课程的课后作业Cos418的GO语言实现思路.由于时间有限,目前只实现了assignment1~2. 在common.go中设置debugEnabled = true,go ...