项目需求分析

项目技术架构

三个应用

  • 小慕读书管理后台(管理电子书)
  • 小慕读书小程序(查阅电子书)
  • 小慕读书h5(提供阅读器)

process

项目目标

  • 完全在本地搭建开发环境
  • 贴近企业真实应用场景

依赖别人提供的 API 将无法真正理解项目的运作逻辑

技术难点分析

登录

  • 用户名密码校验
  • token 生成、校验和路由过滤
  • 前端 token 校验和重定向

电子书上传

  • 文件上传
  • 静态资源服务器

电子书解析

  • epub 原理
  • zip 解压
  • xml 解析

电子书增删改

  • mysql 数据库应用
  • 前后端异常处理

epub 电子书

epub 是一种电子书格式,它的本质是一个 zip 压缩包

Nginx 服务器搭建

安装 nginx

WARNING

使用 macOS 的同学可能会碰到无法写入 /usr/local 问题,后面会提供解决方法

修改配置文件

打开配置文件 nginx.conf:

  • windows 位于安装目录下
  • macOS 位于:/usr/local/etc/nginx/nginx.conf

修改一:添加当前登录用户为owner

user sam owner;
1

修改二:在结尾大括号之前添加:

include /Users/sam/upload/upload.conf;
1

这里 /Users/sam/upload 是资源文件路径,/Users/sam/upload/upload.conf 是额外的配置文件,当前把 /Users/sam/upload/upload.conf 配置文件的内容加入 nginx.conf 也是可行的!

WARNING

使用 windows 的同学可能会碰到路径配置错误导致 500 的情况,最后一节有解法

修改三:添加 /Users/sam/upload/upload.conf 文件,配置如下:

server
{ 
  charset utf-8;
  listen 8089;
  server_name http_host;
  root /Users/sam/upload/;
  autoindex on;
  add_header Cache-Control "no-cache, must-revalidate";
  location / { 
    add_header Access-Control-Allow-Origin *;
  }
}
1
2
3
4
5
6
7
8
9
10
11
12

如果需要加入 https 服务,可以再添加一个 server:

server
{
  listen 443 default ssl;
  server_name https_host;
  root /Users/sam/upload/;
  autoindex on;
  add_header Cache-Control "no-cache, must-revalidate";
  location / {
    add_header Access-Control-Allow-Origin *;
  }
  ssl_certificate /Users/sam/Desktop/https/book_youbaobao_xyz.pem;
  ssl_certificate_key /Users/sam/Desktop/https/book_youbaobao_xyz.key;
  ssl_session_timeout  5m;
  ssl_protocols  SSLv3 TLSv1;
  ssl_ciphers  ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
  ssl_prefer_server_ciphers  on;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  • https证书:/Users/sam/Desktop/https/book_youbaobao_xyz.pem
  • https:私钥:/Users/sam/Desktop/https/book_youbaobao_xyz.key

启动服务

启动 nginx 服务:

sudo nginx
1

重启 nginx 服务:

sudo nginx -s reload
1

停止 nginx 服务:

sudo nginx -s stop
1

检查配置文件是否存在语法错误:

sudo nginx -t
1

访问地址:

  • http: http://localhost:8089
  • https: https://localhost

https 会提示证书有风险,不用理会,直接选择继续访问即可

资源文件

资源文件下载地址:

https://pan.baidu.com/s/1x2N7vl8nd2x6x7FnlQH3Cg#list/path=%2F

提取码:ksjv

将 epub 和 epub2 目录放入 /Users/sam/upload/

常见问题

解决 macOS operation not permitted 问题

macOS 从 El Capitan(10.11)后加入了 Rootless 机制,很多系统目录不再能够随心所欲的读写了,即使设置 root 权限也不行,解决方法:

重启按住 Command+R,进入恢复模式,打开 Terminal:

csrutil disable
1

之后再次进入系统就可以获得修改 /usr 的写入权限了,打开 csrutil 方法是进入恢复模式,在 Terminal 中:

csrutil enable
1

解决 Windows 同学路径配置错误启动出现 500 异常

windows 中不允许在 nginx 配置文件中出现转义字符,比如 \resource 这样的路径会被编译为:esrouce,从而导致 nginx 启动异常,我们可以更换文件夹名称来解决这个问题。

如何申请 https 证书

阿里云提供免费的 https 证书申请,首先需要申请域名,并完成域名实名认证

MySQL 数据库搭建

安装 MySQL

地址:https://dev.mysql.com/downloads/mysql/

安装 Navicat

地址:https://www.navicat.com.cn/products

启动 MySQL

windows 同学参考:https://blog.csdn.net/ycxzuoxin/article/details/80908447

mac 同学参考:https://blog.csdn.net/qq_25628891/article/details/88431942

cd /usr/local/mysql-8.0.13-macos10.14-x86_64/bin
./mysqld
1
2

初始化数据库

创建数据库 book,下载 book.sql:

https://www.youbaobao.xyz/resource/admin/book.sql

执行 book.sql 导入数据

上次更新: 11/15/2019, 11:49:52 PM