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进行解析 ...
随机推荐
- PhpStorm安装及破解流程
下载完以后,把破解的jar包放到bin目录下,更改两个.vmoptions文件 我是安装了一个破解的和汉化包 jet是破解包,resource是汉化包,然后还要修改本地主机地址 例: 0.0.0.0 ...
- [WPF][Rubyer] 写一个自己的 UI 库 (二) - Icon
前言 制作 WPF 的图标包,主要介绍从 iconfont 下载的图标包导入到 WPF 使用: 1. 添加文件 Ruyber 下添加 自定义控件(WPF) Icon.cs.类 IconType.cs. ...
- magento2 pub/static没有adminhtml,frontend的解决方案
(1) 运行php bin/magento setup:static-content:deploy (2) 检查pub/static有没有.htaccess文件 (3) php bin/magento ...
- neutron-server Connection pool is full, discarding connection 连接池过满
参考链接:https://zhiliao.h3c.com/Theme/details/48291 问题: -- ::33.235 WARNING requests.packages.urllib3.c ...
- gorilla/mux 的学习
原文链接:gorilla/mux的学习 源代码: package main import ( "encoding/json" "fmt" "githu ...
- html的鼠标双击,单击,移上,离开,事件捕捉,JavaScript
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 从零开始的SpringBoot项目 ( 三 ) 项目打包( jar包篇 )
一.准备工作 1.工具:Idea , maven 2.首先得保证 pom 有 maven 插件 二.开始打包 找到最右边的Maven Projects,点击进去,选择需要打包的项目,并点击 insta ...
- quartz表(11张)
/* Navicat Premium Data Transfer Source Server : 本地连接 Source Server Type : MySQL Source Server Versi ...
- .net core 3.0 web api 重点设置,主要为了解决axios post不到参数问题
这两天研究.net core 3.0升级,前端vue+axios 后端web api.测试过程中发现post的时候,由于提交的是json对象,后端web api获取不到数据. 今天贴了下解决过程.主要 ...
- python官网打不开
这可能是因为该站点使用过期的或不安全的 TLS 安全设置. 解决:依次打开IE的Internet选项.高级,往下拉,找到安全模块,勾上四个使用:使用SSL 3.0.使用TLS 1.0.使用TLS 1. ...