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. 微信小程序之蓝牙广播信息

    期初第一次做蓝牙开锁的时候遇到的最尖锐的问题就是ios设备如何对获取的广播信息进行读取,大概用了4中方式,都无法解决,最后不得不求助官方人员.给了一个方法,大家可以参考.在此附图: 由于mac地址是6 ...

  2. Linux系统中有趣的命令(可以玩小游戏)

    Linux系统中有趣的命令(可以玩小游戏) 前言 最近,我在看一些关于Linux系统的内容,这里面的内容是真的越学越枯燥,果然学习的过程还是不容易的.记得前几个月初学Linux时,有时候就会碰到小彩蛋 ...

  3. 洛谷P1057 传球游戏 完美错觉(完美错解)

    //作者:pb2 博客:https://www.luogu.com.cn/blog/pb2/ 或 http://www.cnblogs.com/p2blog //博客新闻1:"WPS开机自启 ...

  4. Jmeter 常用函数(26)- 详解 __chooseRandom

    如果你想查看更多 Jmeter 常用函数可以在这篇文章找找哦 https://www.cnblogs.com/poloyy/p/13291704.html 作用 从指定的范围里面取值 语法格式 ${_ ...

  5. python 判断文件和文件夹是否存在、创建文件夹

    原文链接:https://www.cnblogs.com/hushaojun/p/4533241.html >>> import os >>> os.path.ex ...

  6. map[string]interface{} demo

    package main import ( "encoding/json" "fmt" "reflect" ) func demo1() { ...

  7. Adversarial Attack Type I: Cheat Classifiers by Significant Changes

    出于实现目的,翻译原文(侵删) Published in: IEEE Transactions on Pattern Analysis and Machine Intelligence (TPAMI ...

  8. foreach循環體控制

    通常情況下,在程式中的cursor定義之前,整合了l_sql變量后,轉化sql語句時,通過檢查STATUS的值來判斷sql語句是否有錯誤. 語句如:              if STATUS th ...

  9. koa-graphql express-graphql 中如何 定义每一个字段resolver执行函数

    第一种方式:  首先来看一下,官方给出的koa-graphql的例子, ```js var express = require('express'); var {graphqlHTTP} = requ ...

  10. WPF Devexpress控件库中ChartControl--实现不等距x轴

    一.概要 解决问题--ChartControl不等距x轴显示 二.CS代码 用过ChartControl的开发者们应该都知道,ChartControl中设置x轴间距间隔都是固定的数值. 比如(间隔10 ...