koa2-api-scaffold/README.md

742 lines
22 KiB
Markdown
Raw Normal View History

2019-06-07 15:59:39 +08:00
# Koa2 RESTful API 服务器脚手架
2017-01-23 01:12:24 +08:00
2019-06-07 15:59:39 +08:00
这是一个基于 Koa2 的轻量级 RESTful API Server 脚手架,支持 ES6。
2017-01-23 01:12:24 +08:00
2019-06-07 16:01:16 +08:00
**注意:** 因升级 Koa 版本至 2.3.0+,为配合相应的依赖项,故需要 Node.js 版本大于等于 v8.0.0(建议 v11.13.0NPM 大于等于 v5.0.0。建议使用 yarn 代替 npm。
2017-04-07 11:58:35 +08:00
2019-06-07 15:59:39 +08:00
约定使用 JSON 格式传输数据POST、PUT、DELET 方法支持的 Content-Type 为`application/x-www-form-urlencoded、multipart/form-data、application/json`可配置支持跨域。非上传文件推荐 application/x-www-form-urlencoded。通常情况下返回 application/json 格式的 JSON 数据。
2017-01-23 01:12:24 +08:00
2019-06-07 15:59:39 +08:00
可选用 redis 等非关系型数据库。考虑 RESTful API Server 的实际开发需要,这里通过 sequelize.js 作为 PostgreSQL, MySQL, MariaDB, SQLite, MSSQL 关系型数据库的 ORM如无需关系型 ORM`npm remove sequelize -S`,然后删除`src/lib/sequelize.js`文件。
2017-01-23 01:12:24 +08:00
2019-06-07 15:59:39 +08:00
此脚手架只安装了一些和 Koa2 不冲突的搭建 RESTful API Server 的必要插件,附带每一个插件的说明。采用 ESlint 进行语法检查。
2017-01-23 01:12:24 +08:00
2019-06-07 15:59:39 +08:00
因此脚手架主要提供 RESTful API故暂时不考虑前端静态资源处理只提供静态资源访问的基本方法便于访问用户上传到服务器的图片等资源。基本目录结构与 vue-cli 保持一致,可配合 React、AngularJS、Vue.js 等前端框架使用。在 Cordova/PhoneGap、Electron 中使用时需要开启跨域功能。
2017-01-23 01:12:24 +08:00
2019-06-07 15:59:39 +08:00
**免责声明:** 此脚手架仅为方便开发提供基础环境,任何人或组织均可随意克隆使用,使用引入的框架需遵循原作者规定的相关协议(部分框架列表及来源地址在下方)。项目维护者均不对采用此脚手架产生的任何后果负责。
2017-01-23 01:12:24 +08:00
2019-06-07 15:59:39 +08:00
**基于 Vue 2Webpack 4Koa 2 的 SSR 脚手架:**[https://github.com/yi-ge/Vue-SSR-Koa2-Scaffold](https://github.com/yi-ge/Vue-SSR-Koa2-Scaffold)。
2017-01-23 01:12:24 +08:00
2019-06-07 15:59:39 +08:00
## 开发使用说明
2017-01-23 15:39:41 +08:00
2019-04-07 23:03:32 +08:00
```bash
2019-06-07 15:59:39 +08:00
git clone https://github.com/yi-ge/koa2-API-scaffold.git
2017-01-23 01:12:24 +08:00
2019-06-07 15:59:39 +08:00
cd mv koa2-API-scaffold
npm install
npm run dev # 可执行npm start跳过ESlint检查。
2017-01-23 01:12:24 +08:00
```
2017-01-23 15:39:41 +08:00
访问: http://127.0.0.1:3000/
2017-01-23 01:12:24 +08:00
2019-06-07 15:59:39 +08:00
## 调试说明
2017-01-23 01:12:24 +08:00
2019-06-07 15:59:39 +08:00
```bash
npm run dev --debug
2017-01-23 01:12:24 +08:00
Or
2019-06-07 15:59:39 +08:00
npm start --debug
2017-01-23 01:12:24 +08:00
```
2019-06-07 15:59:39 +08:00
支持 Node.js 原生调试功能https://nodejs.org/api/debugger.html
2017-01-23 01:12:24 +08:00
2019-06-07 15:59:39 +08:00
## 开发环境部署
2017-01-23 01:12:24 +08:00
2019-06-07 15:59:39 +08:00
生成 node 直接可以执行的代码到 dist 目录:
2017-01-23 15:39:41 +08:00
2019-06-07 15:59:39 +08:00
```bash
npm run build
2017-01-23 01:12:24 +08:00
```
2019-06-07 15:59:39 +08:00
```bash
npm run production # 生产模式运行
2017-01-23 01:12:24 +08:00
2019-06-07 15:59:39 +08:00
# Or
2017-01-23 01:12:24 +08:00
2019-06-07 15:59:39 +08:00
node dist/app.js
2017-01-23 01:12:24 +08:00
```
2019-06-07 15:59:39 +08:00
### PM2 部署说明
2017-01-23 15:39:41 +08:00
2019-06-07 15:59:39 +08:00
提供了 PM2 部署 RESTful API Server 的示例配置位于“pm2.js”文件中。
2017-01-23 15:39:41 +08:00
2019-06-07 15:59:39 +08:00
```bash
pm2 start pm2.js
2017-01-23 01:12:24 +08:00
```
2019-06-07 15:59:39 +08:00
PM2 配合 Docker 部署说明: http://pm2.keymetrics.io/docs/usage/docker-pm2-nodejs/
2017-01-23 01:12:24 +08:00
2019-06-07 15:59:39 +08:00
### Docker 部署说明
2017-01-23 15:39:41 +08:00
2019-06-07 15:59:39 +08:00
```bash
docker pull node
docker run -itd --name RESTfulAPI -v `pwd`:/usr/src/app -w /usr/src/app node node ./dist/app.js
2017-01-23 01:12:24 +08:00
```
2017-01-23 03:38:59 +08:00
通过'docker ps'查看是否运行成功及运行状态
2017-01-23 01:12:24 +08:00
### Linux/Mac 直接后台运行生产环境代码
2017-01-23 15:39:41 +08:00
2017-01-23 01:12:24 +08:00
有时候为了简单,我们也这样做:
2017-01-23 15:39:41 +08:00
2019-06-07 15:59:39 +08:00
```bash
nohup node ./dist/app.js > logs/out.log &
2017-01-23 01:12:24 +08:00
```
2017-01-23 03:38:59 +08:00
查看运行状态(如果有'node app.js'出现则说明正在后台运行):
2017-01-23 15:39:41 +08:00
2019-06-07 15:59:39 +08:00
```bash
ps aux|grep app.js
2017-01-23 01:12:24 +08:00
```
查看运行日志
2017-01-23 15:39:41 +08:00
2019-06-07 15:59:39 +08:00
```bash
cat logs/out.log
2017-01-23 01:12:24 +08:00
```
监控运行状态
2017-01-23 15:39:41 +08:00
2019-06-07 15:59:39 +08:00
```bash
tail -f logs/out.log
2017-01-23 01:12:24 +08:00
```
2019-06-07 15:59:39 +08:00
### 配合 Vue-cli 部署说明
2017-01-23 15:39:41 +08:00
2019-06-07 15:59:39 +08:00
Vue-cliVue2运行'npm run build'后会在'dist'目录中生成所有静态资源文件。推荐使用 Nginx 处理静态资源以达最佳利用效果,然后通过上述任意一种方法部署 RESTful API 服务器。前后端是完全分离的,请注意 Koa2 RESTful API Server 项目中 config/main.json 里面的跨域配置。
2017-01-23 01:12:24 +08:00
2019-06-07 15:59:39 +08:00
推荐的 Nginx 配置文件:
2017-01-23 15:39:41 +08:00
2019-06-07 15:59:39 +08:00
```text
2017-01-23 01:12:24 +08:00
server
{
listen 80;
listen [::]:80;
server_name abc.com www.abc.com; #绑定域名
index index.html index.htm;
root /www/app/dist; #Vue-cli编译后的dist目录
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 12h;
}
location ~ /\.
{
deny all;
}
access_log off; #访问日志路径
}
```
2017-01-23 15:39:41 +08:00
2019-06-07 15:59:39 +08:00
Docker 中 Nginx 运行命令(将上述配置文件任意命名放置于 nginx_config 目录中即可)
2017-01-23 15:39:41 +08:00
2019-06-07 15:59:39 +08:00
```bash
docker run -itd -p 80:80 -p 443:443 -v `pwd`/nginx_config:/etc/nginx/conf.d nginx
2017-01-23 01:12:24 +08:00
```
2019-06-07 15:59:39 +08:00
### 关于 Token 使用的特别说明JWT 身份认证)
2018-01-13 10:38:06 +08:00
`src\app.js`目录中有一行代码:
2019-06-07 15:59:39 +08:00
`.use(jwt({ secret: publicKey }).unless({ path: [/^\/public|\/user\/login|\/assets/] }))`
2018-01-13 10:38:06 +08:00
2019-06-07 15:59:39 +08:00
在 path 里面的开头路径则不进行身份认证,否则都将进行  鉴权。
2018-01-13 10:38:06 +08:00
前端处理方案:
2019-06-07 15:59:39 +08:00
```javascript
import axios from 'axios'
import { getToken } from './tool'
const DevBaseUrl = 'http://127.0.0.1:8080'
const ProdBashUrl = 'https://xxx.xxx'
2018-01-13 10:38:06 +08:00
let config = {
2019-06-07 15:59:39 +08:00
baseURL: process.env.NODE_ENV !== 'production' ? DevBaseUrl : ProdBashUrl // 配置API接口地址
2018-01-13 10:38:06 +08:00
}
let token = getToken()
if (token) {
2019-06-07 15:59:39 +08:00
config.headers = { Authorization: 'Bearer ' + token }
2018-01-13 10:38:06 +08:00
}
let request = axios.create(config)
// http request 拦截器
axios.interceptors.request.use(
config => {
if (window) {
let token = getToken()
if (token) {
// 判断是否存在token如果存在的话则每个http header都加上token
config.headers.Authorization = `Bearer ${token}`
}
}
// if (config.method === 'get') {
// config.url = config.url + 'timestamp=' + Date.now().toString()
// }
return config
},
err => {
return Promise.reject(err)
}
)
export default request
```
`tool.js`文件
2019-06-07 15:59:39 +08:00
```javascript
2018-01-13 10:38:06 +08:00
// 写 cookies
export let setCookie = function setCookie(name, value, time) {
if (time) {
2019-06-07 15:59:39 +08:00
let strsec = getsec(time)
let exp = new Date()
exp.setTime(exp.getTime() + parseInt(strsec))
document.cookie =
name + '=' + escape(value) + ';expires=' + exp.toGMTString()
2018-01-13 10:38:06 +08:00
} else {
2019-06-07 15:59:39 +08:00
document.cookie = name + '=' + escape(value)
2018-01-13 10:38:06 +08:00
}
2019-06-07 15:59:39 +08:00
}
2018-01-13 10:38:06 +08:00
// 读 cookies
export let getCookie = function(name) {
2019-06-07 15:59:39 +08:00
let reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)')
let arr = document.cookie.match(reg)
return arr ? unescape(arr[2]) : null
}
2018-01-13 10:38:06 +08:00
// 删 cookies
export let delCookie = function(name) {
2019-06-07 15:59:39 +08:00
var exp = new Date()
exp.setTime(exp.getTime() - 1)
var cval = getCookie(name)
2018-01-13 10:38:06 +08:00
if (cval != null) {
2019-06-07 15:59:39 +08:00
document.cookie = name + '=' + cval + ';expires=' + exp.toGMTString()
2018-01-13 10:38:06 +08:00
}
2019-06-07 15:59:39 +08:00
}
2018-01-13 10:38:06 +08:00
// 获取Token
export let getToken = function() {
if (window.sessionStorage && window.sessionStorage.Bearer) {
2019-06-07 15:59:39 +08:00
return window.sessionStorage.Bearer
2018-01-13 10:38:06 +08:00
} else if (window.localStorage && window.localStorage.Bearer) {
2019-06-07 15:59:39 +08:00
return window.localStorage.Bearer
2018-01-13 10:38:06 +08:00
} else if (window.document.cookie) {
2019-06-07 15:59:39 +08:00
return getCookie('Bearer')
2018-01-13 10:38:06 +08:00
}
2019-06-07 15:59:39 +08:00
}
2018-01-13 10:38:06 +08:00
// 设置Token
export let setToken = function(token, rememberTime) {
if (window.sessionStorage) {
2019-06-07 15:59:39 +08:00
window.sessionStorage.Bearer = token
2018-01-13 10:38:06 +08:00
}
if ((rememberTime && window.localStorage) || !window.sessionStorage) {
2019-06-07 15:59:39 +08:00
window.localStorage.Bearer = token
2018-01-13 10:38:06 +08:00
}
if (
2019-06-07 15:59:39 +08:00
window.document.cookie &&
!window.sessionStorage &&
!window.localStorage
2018-01-13 10:38:06 +08:00
) {
if (rememberTime) {
2019-06-07 15:59:39 +08:00
setCookie('Bearer', token, rememberTime)
2018-01-13 10:38:06 +08:00
} else {
2019-06-07 15:59:39 +08:00
setCookie('Bearer', token)
2018-01-13 10:38:06 +08:00
}
}
2019-06-07 15:59:39 +08:00
}
2018-01-13 10:38:06 +08:00
// 删除Token
export let delToken = function() {
if (window.sessionStorage && window.sessionStorage.Bearer) {
2019-06-07 15:59:39 +08:00
window.sessionStorage.removeItem('Bearer')
2018-01-13 10:38:06 +08:00
}
if (window.localStorage && window.localStorage.Bearer) {
2019-06-07 15:59:39 +08:00
window.localStorage.removeItem('Bearer')
2018-01-13 10:38:06 +08:00
}
if (window.document.cookie) {
2019-06-07 15:59:39 +08:00
delCookie('Bearer')
2018-01-13 10:38:06 +08:00
}
2019-06-07 15:59:39 +08:00
}
2018-01-13 10:38:06 +08:00
```
大概原理:
2019-06-07 15:59:39 +08:00
通过某个 API通常是登录 API获取成功后的 Token存于本地然后每次请求的时候在 Header 带上`Authorization: "Bearer " + token`,通常情况下无需担心本地 Token 被破解。
2018-01-13 10:38:06 +08:00
2019-06-07 15:59:39 +08:00
## 引入插件介绍
2017-01-23 01:12:24 +08:00
> 引入插件的版本将会持续更新
引入的插件:
2017-07-16 22:56:44 +08:00
`koa@2 koa-body@2 koa-router@next koa-static2 koa-compose require-directory babel-cli babel-register babel-plugin-transform-runtime babel-preset-es2015 babel-preset-stage-2 gulp gulp-eslint eslint eslint-config-standard eslint-friendly-formatter eslint-plugin-html eslint-plugin-promise nodemailer promise-mysql 等`
2017-01-23 03:38:59 +08:00
2019-06-07 15:59:39 +08:00
**koa2**: HTTP 框架
2017-01-23 01:12:24 +08:00
 Synopsis: HTTP framework.
2017-01-23 15:39:41 +08:00
 From: https://github.com/koajs/koa v2
2017-01-23 03:38:59 +08:00
2019-06-07 15:59:39 +08:00
**koa-body**: body 解析器
2017-01-23 04:59:10 +08:00
 Synopsis: A full-feature koa body parser middleware.
2017-01-23 15:39:41 +08:00
 From: https://github.com/dlau/koa-body
2017-01-23 03:38:59 +08:00
2019-06-07 15:59:39 +08:00
**koa-router**: Koa 路由
2017-01-23 03:38:59 +08:00
 Synopsis: Router middleware for koa.
2017-01-23 15:39:41 +08:00
 From: https://github.com/alexmingoia/koa-router/tree/master/
2017-01-23 03:38:59 +08:00
**koa-static2**: 静态资源中间件
 Synopsis: Middleware for Koa2 to serve a folder under a name declared by user.
2017-01-23 15:39:41 +08:00
 From: https://github.com/Secbone/koa-static2
2017-01-23 03:38:59 +08:00
**koa-compose**: 多个中间件组合成一个
 Synopsis: Compose several middleware into one.
2017-01-23 15:39:41 +08:00
 From: https://github.com/koajs/compose
2017-01-23 04:59:10 +08:00
2017-01-23 03:38:59 +08:00
**require-directory**: 递归遍历指定目录
 Synopsis: Recursively iterates over specified directory.
2017-01-23 15:39:41 +08:00
 From: https://github.com/troygoode/node-require-directory
2017-01-23 01:12:24 +08:00
2019-06-07 15:59:39 +08:00
**babel-cli**: Babel 编译 ES6 代码为 ES5 代码
2017-01-23 01:12:24 +08:00
 Synopsis: Babel is a JavaScript compiler, ES6 to ES5.
 From: https://github.com/babel/babel/tree/master/packages/babel-cli
2019-06-07 15:59:39 +08:00
**babel-register**: Babel 开发环境实时编译 ES6 代码
2017-01-23 01:12:24 +08:00
 Synopsis: Babel hook.
 From: https://github.com/babel/babel/tree/master/packages/babel-cli
2019-06-07 15:59:39 +08:00
**babel-plugin-transform-runtime**: Babel 配置 ES6 的依赖项
2017-01-23 01:12:24 +08:00
**babel-preset-es2015**: 同上
2017-01-23 15:39:41 +08:00
**babel-preset-stage-2**: 同上
2017-01-23 01:12:24 +08:00
**gulp**: 基于流的自动化构建工具
 Synopsis: Gulp is a toolkit for automating painful or time-consuming tasks.
 From: https://github.com/gulpjs/gulp
2019-06-07 15:59:39 +08:00
**gulp-eslint**: gulp 的 ESLint 检查插件
2017-01-23 01:12:24 +08:00
 Synopsis: A gulp plugin for ESLint.
 From: https://github.com/adametry/gulp-eslint
2019-06-07 15:59:39 +08:00
**gulp-nodemon**: 修改 JS 代码后自动重启
2017-01-23 01:12:24 +08:00
 Synopsis: nodemon will watch the files in the directory in which nodemon was started, and if any files change, nodemon will automatically restart your node application.
2017-01-23 15:39:41 +08:00
 From: https://github.com/remy/nodemon
2017-01-23 01:12:24 +08:00
2019-06-07 15:59:39 +08:00
**eslint**: JavaScript 语法检查工具
2017-01-23 01:12:24 +08:00
 Synopsis: A fully pluggable tool for identifying and reporting on patterns in JavaScript.
 From:
2019-06-07 15:59:39 +08:00
**eslint-config-standard**: 一个 ESlint 配置 Synopsis: ESLint Shareable Config for JavaScript Standard Style.
2017-01-23 01:12:24 +08:00
 From: https://github.com/feross/eslint-config-standard
2019-06-07 15:59:39 +08:00
**eslint-friendly-formatter**: 使得 ESlint 提示在 Sublime Text 或 iterm2 中更友好Atom 也有对应的 ESlint 插件。
2017-01-23 03:38:59 +08:00
 Synopsis: A simple formatter/reporter for ESLint that's friendly with Sublime Text and iterm2 'click to open file' functionality
2017-01-23 01:12:24 +08:00
 From: https://github.com/royriojas/eslint-friendly-formatter
2019-06-07 15:59:39 +08:00
**eslint-plugin-html**: 检查 HTML 文件中的 JS 代码规范
2017-01-23 01:12:24 +08:00
 Synopsis: An ESLint plugin to extract and lint scripts from HTML files.
 From: https://github.com/BenoitZugmeyer/eslint-plugin-html
2019-06-07 15:59:39 +08:00
**eslint-plugin-promise**: 检查 JavaScript promises
2017-01-23 15:39:41 +08:00
 Synopsis: Enforce best practices for JavaScript promises. From: https://github.com/xjamundx/eslint-plugin-promise
2017-01-23 01:12:24 +08:00
2019-06-07 15:59:39 +08:00
**eslint-plugin-promise**: ESlint 依赖项
2017-01-23 15:39:41 +08:00
 Synopsis: ESlint Rules for the Standard Linter. From: https://github.com/xjamundx/eslint-plugin-standard
2017-01-23 03:38:59 +08:00
**nodemailer**: 发送邮件
 Synopsis: Send e-mails with Node.JS.
 From: https://github.com/nodemailer/nodemailer
2019-06-07 15:59:39 +08:00
**promise-mysql**: 操作 MySQL 数据库依赖
2017-01-23 03:38:59 +08:00
 Synopsis: Promise Mysql.
2017-01-23 15:39:41 +08:00
 From: https://github.com/lukeb-uk/node-promise-mysql
2017-01-23 04:59:10 +08:00
2019-06-07 15:59:39 +08:00
**sequelize**: 关系型数据库 ORM
2017-01-23 04:59:10 +08:00
 Synopsis: Sequelize is a promise-based ORM for Node.js.
 From: https://github.com/sequelize/sequelize
2019-06-07 15:59:39 +08:00
**mysql**: MySQL 库
2017-01-23 04:59:10 +08:00
 Synopsis: A pure node.js JavaScript Client implementing the MySql protocol.
 From: https://github.com/mysqljs/mysql
2017-01-23 01:12:24 +08:00
2019-06-07 15:59:39 +08:00
支持 Koa2 的中间件列表https://github.com/koajs/koa/wiki
2017-01-23 01:12:24 +08:00
2019-06-07 15:59:39 +08:00
**其它经常配合 Koa2 的插件:**
2017-01-23 03:38:59 +08:00
2019-06-07 15:59:39 +08:00
**koa-session2**: Session 中间件
2017-07-16 22:56:44 +08:00
 Synopsis: Middleware for Koa2 to get/set session.
 From: https://github.com/Secbone/koa-session2
2017-01-23 01:12:24 +08:00
**koa-nunjucks-2**:
2017-01-23 03:38:59 +08:00
一个好用的模版引擎可用于前后端nunjuckshttps://github.com/mozilla/nunjucks
**koa-favicon**:
2019-06-07 15:59:39 +08:00
Koa 的 favicon 中间件https://github.com/koajs/favicon
2017-01-23 03:38:59 +08:00
**koa-server-push**:
2019-06-07 15:59:39 +08:00
HTTP2 推送中间件https://github.com/silenceisgolden/koa-server-push
2017-01-23 03:38:59 +08:00
2019-06-07 15:59:39 +08:00
**koa-convert**: 转换旧的中间件支持 Koa2
2017-01-23 03:38:59 +08:00
 Synopsis: Convert koa generator-based middleware to promise-based middleware.
2017-01-23 15:39:41 +08:00
 From: https://github.com/koajs/convert
2017-01-23 03:38:59 +08:00
**koa-logger**: 请求日志输出,需要配合上面的插件使用
 Synopsis: Development style logger middleware for Koa.
 From: https://github.com/koajs/logger
2017-01-23 01:12:24 +08:00
2017-01-23 04:59:10 +08:00
**koa-onerror**:
2019-06-07 15:59:39 +08:00
Koa 的错误拦截中间件需要配合上面的插件使用https://github.com/koajs/onerror
2017-01-23 04:59:10 +08:00
2017-01-24 23:19:44 +08:00
**koa-multer**: 处理数据中间件
 Synopsis: Multer is a node.js middleware for handling multipart/form-data for koa.
 From: https://github.com/koa-modules/multer
2019-06-07 15:59:39 +08:00
## 目录结构说明
2017-01-23 01:12:24 +08:00
```bash
.
├── README.md
├── .babelrc # Babel 配置文件
├── .editorconfig # 编辑器风格定义文件
├── .eslintignore # ESlint 忽略文件列表
├── .eslintrc.js # ESlint 配置文件
├── .gitignore # Git 忽略文件列表
├── gulpfile.js # Gulp配置文件
├── package.json # 描述文件
├── pm2.js # pm2 部署示例文件
├── build # build 入口目录
│   └── dev-server.js # 开发环境 Babel 实时编译入口
├── src # 源代码目录,编译后目标源代码位于 dist 目录
2017-01-23 03:38:59 +08:00
│   ├── app.js # 入口文件
2017-01-23 15:39:41 +08:00
│   ├── config.js # 主配置文件(*谨防泄密!)
2017-01-23 03:38:59 +08:00
│   ├── plugin # 插件目录
│   └── smtp_sendemail # 示例插件 - 发邮件
│   ├── tool # 工具目录
│   ├── PluginLoader.js # 插件引入工具
│   └── Common.js # 示例插件 - 发邮件
│   ├── lib # 库目录
2017-01-23 01:12:24 +08:00
│   ├── controllers # 控制器
│   ├── models # 模型
│   ├── routes # 路由
2017-01-23 03:38:59 +08:00
│   └── services # 服务
├── assets # 静态资源目录
2017-01-23 01:12:24 +08:00
└── logs # 日志目录
```
2019-06-07 15:59:39 +08:00
## 集成 NUXT 请求时身份认证说明
2017-07-19 19:38:53 +08:00
```
import Vue from 'vue'
import axios from 'axios'
const DevBaseUrl = 'http://127.0.0.1:3000'
const ProdBashUrl = 'https://api.xxx.com'
let config = {
baseURL: process.env.NODE_ENV !== 'production' ? DevBaseUrl : ProdBashUrl // 配置API接口地址
}
if (process.env.VUE_ENV !== 'server') {
let token = getToken() // 此函数自行实现
if (token) {
config.headers = {Authorization: 'Bearer ' + token}
}
}
let request = axios.create(config)
// http request 拦截器
axios.interceptors.request.use(
(config) => {
if (window) {
let token = getToken()
if (token) { // 判断是否存在token如果存在的话则每个http header都加上token
config.headers.Authorization = `Bearer ${token}`
}
}
return config
},
(err) => {
return Promise.reject(err)
}
)
Vue.prototype.$request = request
```
2019-06-07 15:59:39 +08:00
## 各类主流框架调用 RESTful API 的示例代码(仅供参考)
2017-01-23 01:12:24 +08:00
2019-06-07 15:59:39 +08:00
### AngularJS (Ionic 同)
2017-01-23 15:39:41 +08:00
2019-06-07 15:59:39 +08:00
```javascript
2017-01-23 01:12:24 +08:00
$http({
2019-06-07 15:59:39 +08:00
method: 'post',
url: 'http://localhost:3000/xxx',
data: { para1: 'para1', para2: 'para2' },
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
})
.success(function(data) {
// do something
})
.error(function(data) {
// do something
})
2017-01-23 15:39:41 +08:00
```
2017-01-23 01:12:24 +08:00
### jQuery
2017-01-23 15:39:41 +08:00
2019-06-07 15:59:39 +08:00
```javascript
2017-01-23 01:12:24 +08:00
$.ajax({
cache: false,
type: 'POST',
2017-01-23 03:38:59 +08:00
url: 'http://localhost:3000/xxx',
2017-01-23 01:12:24 +08:00
data: {
2019-06-07 15:59:39 +08:00
para1: para1
2017-01-23 01:12:24 +08:00
},
async: false,
2017-01-23 03:38:59 +08:00
dataType: 'json',
2019-06-07 15:59:39 +08:00
success: function(result) {},
error: function(err) {
console.log(err)
2017-01-23 01:12:24 +08:00
}
2017-01-23 03:38:59 +08:00
})
2017-01-23 01:12:24 +08:00
// 上传文件
//创建FormData对象
2017-01-23 03:38:59 +08:00
var data = new FormData()
2017-01-23 01:12:24 +08:00
//为FormData对象添加数据
//
2019-06-07 15:59:39 +08:00
$.each($('#inputfile')[0].files, function(i, file) {
2017-01-23 03:38:59 +08:00
data.append('upload_file', file)
})
2017-01-23 01:12:24 +08:00
$.ajax({
url: 'http://127.0.0.1:3000/api/upload_oss_img_demo',
type: 'POST',
data: data,
cache: false,
2019-06-07 15:59:39 +08:00
contentType: false, //不可缺
processData: false, //不可缺
success: function(data) {
2017-01-23 03:38:59 +08:00
console.log(data)
if (data.result == 'ok') {
$('#zzzz').attr('src', data.img_url)
2017-01-23 01:12:24 +08:00
}
}
2017-01-23 03:38:59 +08:00
})
2017-01-23 01:12:24 +08:00
```
### MUI
2017-01-23 15:39:41 +08:00
2019-06-07 15:59:39 +08:00
```javascript
mui.ajax({
url: 'http://localhost:3000/xxx',
dataType: 'json',
success: function(data) {},
error: function(data) {
console.log('error!')
2017-01-23 01:12:24 +08:00
}
2017-01-23 03:38:59 +08:00
})
2017-01-23 01:12:24 +08:00
```
### JavaScript
2017-01-23 15:39:41 +08:00
2019-06-07 15:59:39 +08:00
```javascript
var xhr = new XMLHttpRequest()
xhr.open('POST', 'http://localhost:3000/xxx', true) //POST或GETtrue异步或 false同步
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded')
xhr.withCredentials = true
xhr.onreadystatechange = function() {
if ((obj.readyState == 4 && obj.status == 200) || obj.status == 304) {
var gotServices = JSON.parse(xhr.responseText)
} else {
console.log('ajax失败了')
2017-01-23 01:12:24 +08:00
}
2019-06-07 15:59:39 +08:00
}
xhr.send({ para1: para1 })
2017-01-23 01:12:24 +08:00
```
### vue-resource
2017-01-23 15:39:41 +08:00
2017-01-23 01:12:24 +08:00
https://github.com/pagekit/vue-resource
2017-01-23 15:39:41 +08:00
2019-06-07 15:59:39 +08:00
```javascript
2017-01-23 01:12:24 +08:00
// global Vue object
Vue.http.post('/someUrl', [body], {
2017-01-23 03:38:59 +08:00
headers: {'Content-type', 'application/x-www-form-urlencoded'}
}).then(successCallback, errorCallback)
2017-01-23 01:12:24 +08:00
```
### fetch
2017-01-23 15:39:41 +08:00
2017-01-23 01:12:24 +08:00
https://github.com/github/fetch
2017-01-23 15:39:41 +08:00
2019-06-07 15:59:39 +08:00
```javascript
2017-01-23 01:12:24 +08:00
fetch('/users', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
name: 'Hubot',
login: 'hubot',
})
}).then(function(response) {
// response.text()
}).then(function(body) {
// body
})
// 文件上传
2017-01-23 03:38:59 +08:00
var input = document.querySelector('input[type='file']')
2017-01-23 01:12:24 +08:00
var data = new FormData()
data.append('file', input.files[0])
data.append('user', 'hubot')
fetch('/avatars', {
method: 'POST',
body: data
})
```
### superagent
2017-01-23 15:39:41 +08:00
2017-01-23 01:12:24 +08:00
https://github.com/visionmedia/superagent
2017-01-23 15:39:41 +08:00
2019-06-07 15:59:39 +08:00
```javascript
2017-01-23 01:12:24 +08:00
request.post('/user')
.set('Content-Type', 'application/json')
2017-01-23 03:38:59 +08:00
.send('{'name':'tj','pet':'tobi'}')
2017-01-23 01:12:24 +08:00
.end(callback)
```
2017-02-03 13:46:11 +08:00
### request
https://github.com/request/request
2019-06-07 15:59:39 +08:00
```javascript
2017-02-03 13:46:11 +08:00
request.post('/api').form({key:'value'}), function(err,httpResponse,body){ /* ... */ })
```
2019-06-07 15:59:39 +08:00
在 React 中可以将上述任意方法其置于 componentDidMount()中Vue.js 同理。
2017-01-23 01:12:24 +08:00
2019-06-07 15:59:39 +08:00
## 彻底移除 ESlint 方法
2017-01-23 15:39:41 +08:00
2019-06-07 15:59:39 +08:00
删除 package.json 的 devDependencies 中所有 eslint 开头的插件,根目录下的“.eslintignore、.eslintrc.js”文件并且修改 package.json 的 dev 为:
2017-01-23 15:39:41 +08:00
2019-06-07 15:59:39 +08:00
```bash
2017-01-23 03:38:59 +08:00
'dev': 'gulp start'
2017-01-23 01:12:24 +08:00
```
2017-01-23 15:39:41 +08:00
2019-06-07 15:59:39 +08:00
删除 gulpfile.js 中的 lint、eslint_start 两个任务,并且把 default 改为“gulp.task('default', ['start']”。
## 更新说明
_v1.0.2 2019 年 06 月 07 日 15:15:37_
1. 解决`WS-2019-0064`警告,升级依赖项版本。
2. 规范路由引入方式,增加不同写法的示例。
2017-02-07 16:05:12 +08:00
2019-06-07 15:59:39 +08:00
_v1.0.1 2019 年 06 月 02 日 21:54:00_
2019-06-02 21:55:11 +08:00
2019-06-07 15:59:39 +08:00
1. 添加 Dockerfile
2019-06-02 21:55:11 +08:00
2019-06-07 15:59:39 +08:00
_v1.0.0 2019 年 04 月 07 日 21:19:59_
2019-04-07 23:03:32 +08:00
1. 升级依赖项版本node11.13.0)。
2019-06-07 15:59:39 +08:00
2. 添加 Jest。
2017-03-01 23:05:39 +08:00
2019-06-07 15:59:39 +08:00
_v0.2.6 2018 年 03 月 24 日 22:16:43_
2018-03-24 22:17:29 +08:00
2019-06-07 15:59:39 +08:00
1. 升级依赖项版本node9.9.0)。
2018-03-24 22:17:29 +08:00
2019-06-07 15:59:39 +08:00
_v0.2.5 2018 年 01 月 13 日 10:37:29_
2018-01-13 10:38:06 +08:00
2019-06-07 15:59:39 +08:00
1. 升级依赖项版本node9.3.0)。
2. 添加了 Token 使用说明。
2018-01-13 10:38:06 +08:00
2019-06-07 15:59:39 +08:00
_v0.2.4 2017 年 12 月 01 日 14:16:03_
2017-12-01 14:26:09 +08:00
2019-06-07 15:59:39 +08:00
1. 升级依赖项版本node9.2.0)。
2017-12-01 14:26:09 +08:00
2019-06-07 15:59:39 +08:00
_v0.2.3 2017 年 10 月 23 日 23:26:56_
2017-10-23 23:27:24 +08:00
2019-06-07 15:59:39 +08:00
1. 升级依赖项版本node8.7.0)。
2017-10-23 23:27:24 +08:00
2019-06-07 15:59:39 +08:00
_v0.2.2 2017 年 09 月 17 日 17:45:07_
2017-09-17 17:46:07 +08:00
2019-06-07 15:59:39 +08:00
1. 升级依赖项版本(在 node8.5.0 测试)。
2017-09-17 17:46:07 +08:00
2019-06-07 15:59:39 +08:00
_v0.2.1 2017 年 07 月 19 日 19:38:19_
2017-07-19 19:40:45 +08:00
2019-06-07 15:59:39 +08:00
1. 移除 MySQL 模块(重复了)。
2. 添加更详细的说明。
2017-07-19 19:40:45 +08:00
2019-06-07 15:59:39 +08:00
_v0.2 2017 年 07 月 16 日 22:48:34_
2017-07-16 22:56:44 +08:00
2019-06-07 15:59:39 +08:00
1. 升级 koa 为 2.3.0 版本。
2. 将 koa-session2 替换为 koa-jwt添加了 jsonwebtoken。
3. 升级了以下依赖的版本: koa@2.3.0, koa-body@2.3.0, koa-router@7.2.1, babel-cli@6.24.1, babel-preset-es2015@6.24.1, babel-preset-stage-2@6.24.1, babel-register@6.24.1, eslint-plugin-promise@3.5.0, koa-compose@4.0.0, nodemailer@4.0.1, sequelize@4.3.2, eslint@4.2.0, eslint-config-standard@10.2.1, eslint-friendly-formatter@3.0.0, eslint-plugin-html@3.1.0, gulp-eslint@4.0.0, koa-logger@3.0.1
2017-07-16 22:56:44 +08:00
2019-06-07 15:59:39 +08:00
_v0.1 2017 年 04 月 07 日 11:46:02_
2017-04-07 11:54:19 +08:00
2019-06-07 15:59:39 +08:00
1. 升级 koa 为 2.2.0 版本。
2. 升级了以下依赖版本到“建议版本”:
2017-04-07 11:54:19 +08:00
```
依赖项 原版本 建议版本 最新版本 项目
eslint 3.18.0 3.19.0 3.19.0 koa2-API-scaffold
2017-04-07 12:46:30 +08:00
eslint-plugin-standard 2.1.1 3.0.1 3.0.1 koa2-API-scaffold
2017-04-07 11:54:19 +08:00
koa-router 7.1.0 7.1.1 7.1.1 koa2-API-scaffold
promise-mysql 3.0.0 3.0.1 3.0.1 koa2-API-scaffold
2017-04-07 12:46:30 +08:00
sequelize                3.30.2  3.30.4  3.30.4  koa2-API-scaffold
2017-04-07 11:54:19 +08:00
```
2019-06-07 15:59:39 +08:00
_v0.0.9_1 2017 年 03 月 30 日 15:51:03_
2017-03-30 15:51:52 +08:00
2019-06-07 15:59:39 +08:00
1. 完善了 gulpfile.js不是重要的更新。
2017-03-30 15:51:52 +08:00
2019-06-07 15:59:39 +08:00
_v0.0.9 2017 年 03 月 27 日 17:25:58_
2017-03-27 17:28:08 +08:00
2019-06-07 15:59:39 +08:00
1. 修改了`src/controllers/upload.js`文件,添加了上传文件示例代码。
2. 修复了`src/lib/mysql.js`引用路径错误的 BUG。
3. 修改了`src/controllers/api.js`GET 参数获取的示例。
2017-03-27 17:28:08 +08:00
2019-06-07 15:59:39 +08:00
_v0.0.8 2017 年 03 月 01 日 23:03:44_
1、在 src/app.js 18 行 后添加了
2017-03-27 17:28:08 +08:00
2017-03-01 23:05:39 +08:00
```
jsonLimit: '20mb',
formLimit: '10mb',
textLimit: '20mb',
```
2017-03-27 17:28:08 +08:00
2017-03-01 23:05:39 +08:00
以免表单过长导致提交失败。
2019-06-07 15:59:39 +08:00
_v0.0.7 2017 年 02 月 18 日 19:01:48_
1、修改了 index.js、config.js详情请看 github 记录),完善了对跨域的处理。 更新建议:对应 github 修改记录同步修改,方便将来部署调试。
2017-02-18 19:02:15 +08:00
2019-06-07 15:59:39 +08:00
_v0.0.6 2017 年 02 月 17 日 21:17:23_
1、修改了 src/lib/sequelize.js 文件,添加了对 utf8mb4 的支持(要求 MySQL 版本>5.5)。
2017-02-12 01:28:00 +08:00
2019-06-07 15:59:39 +08:00
_v0.0.5 2017 年 02 月 12 日 01:25:34_
1、修改了 gulpfile.js 文件,在更改文件热重启的时候无需检查全部文件,仅检查改动文件,开发速度更快。
2、修改了 package.json 中"start"项的值为"gulp nodemon"配合 gulpfile.js 文件的修改。
2017-02-07 16:05:12 +08:00
2019-06-07 15:59:39 +08:00
_v0.0.4 2017 年 02 月 07 日 15:57:17_
2017-02-07 16:05:12 +08:00
1、修改了部分配置文件的配置方法使之更为规范老版本用户无须理会对程序没有影响
2019-06-07 15:59:39 +08:00
2、修改了 eslintrc.js 文件中的 JavaScript 版本配置,改为 ES8兼容 async、await。
3、修改 gulpfile.js 文件第 12 行,检查`src/**/*.js`文件。