v0.0.5 修复bug

This commit is contained in:
yi-ge 2017-01-23 15:39:41 +08:00
parent ef84eb8428
commit 502ff4aa94
7 changed files with 75 additions and 58 deletions

View File

@ -1,4 +1,5 @@
# Koa2 RESTful API 服务器脚手架
Koa2 RESTful API 服务器脚手架
=============================
这是一个基于Koa2的轻量级Resultful API Server脚手架支持ES6。
@ -16,7 +17,9 @@
China大陆用户请自行优化网络。
## 开发使用说明
开发使用说明
------------
```
$ git clone https://github.com/yi-ge/koa2-API-scaffold.git
@ -24,26 +27,14 @@ $ cd mv koa2-API-scaffold
$ npm install # 安装依赖项
$ npm run dev # 可执行npm start跳过ESlint检查。
```
访问: http://127.0.0.1:3000/
另外一种方式如果你使用git仓库管理你的代码
调试说明
--------
```
$ git clone https://github.com/yi-ge/koa2-API-scaffold.git
$ mv koa2-API-scaffold `您的项目名称`
$ cd `您的项目名称`
$ rm -rf .git
$ git init
$ git remote add origin `您的git仓库地址`
$ npm install
$ npm run dev # 可执行npm start跳过ESlint检查。
```
## 调试说明
```
$ npm start --debug
$ npm run dev --debug
Or
@ -52,9 +43,11 @@ $ npm start --debug
支持Node.js原生调试功能https://nodejs.org/api/debugger.html
## 开发环境部署
开发环境部署
------------
生成node直接可以执行的代码到dist目录
```
$ npm run build
```
@ -68,15 +61,17 @@ $ node dist/app.js
```
### PM2部署说明
提供了PM2部署RESTful API Server的示例配置位于“pm2.js”文件中。
```
$ pm2 start pm2.js
```
PM2配合Docker部署说明
http://pm2.keymetrics.io/docs/usage/docker-pm2-nodejs/
PM2配合Docker部署说明 http://pm2.keymetrics.io/docs/usage/docker-pm2-nodejs/
### Docker部署说明
```
$ docker pull node
$ docker run -itd --name RESTfulAPI -v `pwd`:/usr/src/app -w /usr/src/app node node ./dist/app.js
@ -85,30 +80,37 @@ $ docker run -itd --name RESTfulAPI -v `pwd`:/usr/src/app -w /usr/src/app node n
通过'docker ps'查看是否运行成功及运行状态
### Linux/Mac 直接后台运行生产环境代码
有时候为了简单,我们也这样做:
```
$ nohup node ./dist/app.js > logs/out.log &
```
查看运行状态(如果有'node app.js'出现则说明正在后台运行):
```
$ ps aux|grep app.js
```
查看运行日志
```
$ cat logs/out.log
```
监控运行状态
```
$ tail -f logs/out.log
```
### 配合Vue-cli部署说明
Vue-cliVue2运行'npm run build'后会在'dist'目录中生成所有静态资源文件。推荐使用Nginx处理静态资源以达最佳利用效果然后通过上述任意一种方法部署RESTful API服务器。前后端是完全分离的请注意Koa2 RESTful API Server项目中config/main.json里面的跨域配置。
推荐的Nginx配置文件
```
server
{
@ -136,21 +138,24 @@ server
access_log off; #访问日志路径
}
```
Docker中Nginx运行命令(将上述配置文件任意命名放置于nginx_config目录中即可)
```
$ docker run -itd -p 80:80 -p 443:443 -v `pwd`/nginx_config:/etc/nginx/conf.d nginx
```
## 引入插件介绍
引入插件介绍
------------
> 引入插件的版本将会持续更新
引入的插件:
`` koa@2 koa-body@2 koa-router@next koa-session2 koa-static2 koa-compose koa-multer 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 ``
`koa@2 koa-body@2 koa-router@next koa-session2 koa-static2 koa-compose koa-multer 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`
**koa2**: HTTP框架
 Synopsis: HTTP framework.
 From: https://github.com/koajs/koa v2
 From: https://github.com/koajs/koa v2
**koa-body**: body解析器
 Synopsis: A full-feature koa body parser middleware.
@ -208,8 +213,7 @@ $ docker run -itd -p 80:80 -p 443:443 -v `pwd`/nginx_config:/etc/nginx/conf.d ng
 Synopsis: A fully pluggable tool for identifying and reporting on patterns in JavaScript.
 From:
**eslint-config-standard**: 一个ESlint配置
 Synopsis: ESLint Shareable Config for JavaScript Standard Style.
**eslint-config-standard**: 一个ESlint配置 Synopsis: ESLint Shareable Config for JavaScript Standard Style.
 From: https://github.com/feross/eslint-config-standard
**eslint-friendly-formatter**: 使得ESlint提示在Sublime Text或iterm2中更友好Atom也有对应的ESlint插件。
@ -221,12 +225,10 @@ $ docker run -itd -p 80:80 -p 443:443 -v `pwd`/nginx_config:/etc/nginx/conf.d ng
 From: https://github.com/BenoitZugmeyer/eslint-plugin-html
**eslint-plugin-promise**: 检查JavaScript promises
 Synopsis: Enforce best practices for JavaScript promises.
 From: https://github.com/xjamundx/eslint-plugin-promise
 Synopsis: Enforce best practices for JavaScript promises. From: https://github.com/xjamundx/eslint-plugin-promise
**eslint-plugin-promise**: ESlint依赖项
 Synopsis: ESlint Rules for the Standard Linter.
 From: https://github.com/xjamundx/eslint-plugin-standard
 Synopsis: ESlint Rules for the Standard Linter. From: https://github.com/xjamundx/eslint-plugin-standard
**nodemailer**: 发送邮件
 Synopsis: Send e-mails with Node.JS.
@ -268,7 +270,8 @@ HTTP2推送中间件https://github.com/silenceisgolden/koa-server-push
**koa-onerror**:
Koa的错误拦截中间件需要配合上面的插件使用https://github.com/koajs/onerror
## 目录结构说明
目录结构说明
------------
```bash
.
@ -283,10 +286,9 @@ Koa的错误拦截中间件需要配合上面的插件使用https://github
├── pm2.js # pm2 部署示例文件
├── build # build 入口目录
│   └── dev-server.js # 开发环境 Babel 实时编译入口
├── config
│   └── main.js # 主配置文件(*谨防泄密!)
├── src # 源代码目录,编译后目标源代码位于 dist 目录
│   ├── app.js # 入口文件
│   ├── config.js # 主配置文件(*谨防泄密!)
│   ├── plugin # 插件目录
│   └── smtp_sendemail # 示例插件 - 发邮件
│   ├── tool # 工具目录
@ -301,9 +303,11 @@ Koa的错误拦截中间件需要配合上面的插件使用https://github
└── logs # 日志目录
```
## 各类主流框架调用RESTful API的示例代码仅供参考
各类主流框架调用RESTful API的示例代码仅供参考
-------------------------------------------------
### AngularJS (Ionic同)
```
$http({
method: 'post',
@ -315,9 +319,10 @@ $http({
}).success(function (data) {
}).error(function (data) {
})
```
```
### jQuery
```
$.ajax({
cache: false,
@ -361,6 +366,7 @@ $.ajax({
```
### MUI
```
mui.ajax({ url: 'http://localhost:3000/xxx', dataType: 'json',
success: function(data){
@ -373,6 +379,7 @@ mui.ajax({ url: 'http://localhost:3000/xxx', dataType: 'json',
```
### JavaScript
```
var xhr = new XMLHttpRequest()
xhr.open('POST', 'http://localhost:3000/xxx', true) //POST或GETtrue异步或 false同步
@ -389,7 +396,9 @@ mui.ajax({ url: 'http://localhost:3000/xxx', dataType: 'json',
```
### vue-resource
https://github.com/pagekit/vue-resource
```
// global Vue object
Vue.http.post('/someUrl', [body], {
@ -398,7 +407,9 @@ Vue.http.post('/someUrl', [body], {
```
### fetch
https://github.com/github/fetch
```
fetch('/users', {
method: 'POST',
@ -429,7 +440,9 @@ fetch('/avatars', {
```
### superagent
https://github.com/visionmedia/superagent
```
request.post('/user')
.set('Content-Type', 'application/json')
@ -439,9 +452,13 @@ request.post('/user')
在React中可以将上述任意方法其置于componentDidMount()中Vue.js同理。
## 彻底移除ESlint方法
彻底移除ESlint方法
------------------
删除package.json的devDependencies中所有eslint开头的插件根目录下的“.eslintignore、.eslintrc.js”文件并且修改package.json的dev为
```
'dev': 'gulp start'
```
删除gulpfile.js中的lint、eslint_start两个任务并且把default改为“gulp.task('default', ['start']”。

View File

@ -2,7 +2,7 @@ import Koa2 from 'koa'
import KoaBody from 'koa-body'
import KoaSession from 'koa-session2'
import KoaStatic from 'koa-static2'
import { SystemConfig } from '../config/main.js'
import { SystemConfig } from './config'
import path from 'path'
import MainRoutes from './routes/main-routes'
import ErrorRoutes from './routes/error-routes'

View File

@ -1,5 +1,5 @@
import mysql from 'promise-mysql'
import { SystemConfig } from '../config.js'
import { SystemConfig } from '../config'
import { SqlFormat } from '../tool/common_tool.js'
let pool = mysql.createPool({

View File

@ -1,5 +1,5 @@
import Sequelize from 'sequelize'
import { SystemConfig } from '../config/main'
import { SystemConfig } from '../config'
export default new Sequelize(SystemConfig.mysql_database, SystemConfig.mysql_user, SystemConfig.mysql_password, {
host: SystemConfig.mysql_host,

View File

@ -1,5 +1,5 @@
import nodemailer from 'nodemailer'
import { SendEmail } from '../../../config/main'
import { SendEmail } from '../../config'
// 发送Email目前使用的是阿里云SMTP发送邮件
// receivers 目标邮箱,可以用英文逗号分隔多个。(我没试过)

View File

@ -1,6 +1,6 @@
import {
SystemConfig
} from '../config/main.js'
} from '../config'
// 截取字符串,多余的部分用...代替
export let setString = (str, len) => {