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博客项目[上线与配置]

目录结构

    |-- 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博客项目[接口]的更多相关文章

  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博客项目[数据存储]

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

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

    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. Node.js调用百度地图Web服务API的Geocoding接口进行点位反地理信息编码

    (从我的新浪博客上搬来的,做了一些修改.) 最近迷上了node.js以及JavaScript.现在接到一个活,要解析一个出租车点位数据的地理信息.于是就想到使用Node.js调用百度地图API进行解析 ...

随机推荐

  1. you-get的一点修改

    一直用you-get这个python写的开源软件下载一些视频网站的视频(主要是太烦不断插入的广告),最近看了点python,就对于自己觉得不够方便的地方,尝试修改.因为感觉他的github上提交修改建 ...

  2. 转圈游戏C++

    转圈游戏 问题描述 n 个小伙伴(编号从 0 到 n-1)围坐一圈玩游戏.按照顺时针方向给 n 个位置编号,从0 到 n-1.最初,第 0 号小伙伴在第 0 号位置,第 1 号小伙伴在第 1 号位置, ...

  3. BIGI行情http请求实时行情数据方式

    BIGI行情http请求实时行情数据方式 新浪财经文华财经并非实时行情数据源,所以获取的行情数据源也并非实时的.以下介绍的方法和新浪财经获取行情数据源的方法是一致的.需要实时行情数据源可以向BIGI行 ...

  4. 第七天Scrum冲刺博客

    1.会议照片 2.项目进展 团队成员 昨日计划任务 今日计划任务 梁天龙  学习课程页面  建议页面 黄岳康  定义个人课程  登陆页面 吴哲翰  完成页面的与后端的沟通交流  继续保持确认功能齐全 ...

  5. tp6 不能使用vendor

    从thinkphp 5.1.x后vendor的使用方法发生变化,文档又没有详细说明.官方真的太坑了! 在thinkPHP 5.1.X后新版取消了Loader::import方法以及import和ven ...

  6. Shader Graph

    About Shader Graph https://docs.unity3d.com/Packages/com.unity.shadergraph@7.3/manual/index.html uni ...

  7. oeasy 教您玩转linux010101查看内核uname

    linux([?l?n?ks]) 是什么????? 咱们这次讲点什么呢?这次咱们讲讲这个 linux([?l?n?ks]),什么是 linux([?l?n?ks])呢?这linux([?l?n?ks] ...

  8. Lua_C_C#

    lua调用c函数 https://www.cnblogs.com/etangyushan/p/4384368.html Lua中调用C函数 https://www.cnblogs.com/sifenk ...

  9. android 捕获未try的异常、抓取崩溃日志

    1.Thread.UncaughtExceptionHandler java里有很多异常如:空指针异常,越界异常,数值转换异常,除0异常,数据库异常等等.如果自己没有try / catch 那么线程就 ...

  10. gcd(a,b) 复杂度证明

    (b,a%b) a%b<=min(b,a%b)/2 a>=b时每次至少缩减一半 a<b时下次a>b 所以复杂度最多2log(max(a,b)) 证明:a%b<=min(a ...