From 502ff4aa94c46d3c748c995620edd4984cc471e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E8=BD=B6?= Date: Mon, 23 Jan 2017 15:39:41 +0800 Subject: [PATCH] =?UTF-8?q?v0.0.5=20=E4=BF=AE=E5=A4=8Dbug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 123 ++++++++++++++++------------ src/app.js | 2 +- config/main.js => src/config.js | 0 src/lib/mysql.js | 2 +- src/lib/sequelize.js | 2 +- src/plugins/smtp_sendemail/index.js | 2 +- src/tool/Common.js | 2 +- 7 files changed, 75 insertions(+), 58 deletions(-) rename config/main.js => src/config.js (100%) diff --git a/README.md b/README.md index 8eb2504..9caaf6f 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,7 @@ -# Koa2 RESTful API 服务器脚手架 +Koa2 RESTful API 服务器脚手架 +============================= -这是一个基于Koa2的轻量级Resultful API Server脚手架,支持ES6。 +这是一个基于Koa2的轻量级Resultful API Server脚手架,支持ES6。 约定使用JSON格式传输数据,POST、PUT、DELET方法支持的Content-Type为application/x-www-form-urlencoded和multipart/form-data、application/json,可配置支持跨域。非上传文件推荐application/x-www-form-urlencoded。通常情况下返回application/json格式的JSON数据。 @@ -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-cli(Vue2)运行'npm run build'后会在'dist'目录中生成所有静态资源文件。推荐使用Nginx处理静态资源以达最佳利用效果,然后通过上述任意一种方法部署RESTful API服务器。前后端是完全分离的,请注意Koa2 RESTful API Server项目中config/main.json里面的跨域配置。 推荐的Nginx配置文件: + ``` server { @@ -136,49 +138,52 @@ 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. - From: https://github.com/dlau/koa-body + From: https://github.com/dlau/koa-body **koa-router**: Koa路由  Synopsis: Router middleware for koa. - From: https://github.com/alexmingoia/koa-router/tree/master/ + From: https://github.com/alexmingoia/koa-router/tree/master/ **koa-session2**: Session中间件  Synopsis: Middleware for Koa2 to get/set session. - From: https://github.com/Secbone/koa-session2 + From: https://github.com/Secbone/koa-session2 **koa-static2**: 静态资源中间件  Synopsis: Middleware for Koa2 to serve a folder under a name declared by user. - From: https://github.com/Secbone/koa-static2 + From: https://github.com/Secbone/koa-static2 **koa-compose**: 多个中间件组合成一个  Synopsis: Compose several middleware into one. - From: https://github.com/koajs/compose + From: https://github.com/koajs/compose **koa-multer**: 处理数据中间件  Synopsis: Multer is a node.js middleware for handling multipart/form-data for koa. - From: https://github.com/koa-modules/multer + From: https://github.com/koa-modules/multer **require-directory**: 递归遍历指定目录  Synopsis: Recursively iterates over specified directory. - From: https://github.com/troygoode/node-require-directory + From: https://github.com/troygoode/node-require-directory **babel-cli**: Babel编译ES6代码为ES5代码  Synopsis: Babel is a JavaScript compiler, ES6 to ES5. @@ -190,7 +195,7 @@ $ docker run -itd -p 80:80 -p 443:443 -v `pwd`/nginx_config:/etc/nginx/conf.d ng **babel-plugin-transform-runtime**: Babel配置ES6的依赖项 **babel-preset-es2015**: 同上 -**babel-preset-stage-2**: 同上 +**babel-preset-stage-2**: 同上 **gulp**: 基于流的自动化构建工具  Synopsis: Gulp is a toolkit for automating painful or time-consuming tasks. @@ -202,14 +207,13 @@ $ docker run -itd -p 80:80 -p 443:443 -v `pwd`/nginx_config:/etc/nginx/conf.d ng **gulp-nodemon**: 修改JS代码后自动重启  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. - From: https://github.com/remy/nodemon + From: https://github.com/remy/nodemon **eslint**: JavaScript语法检查工具  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. @@ -234,7 +236,7 @@ $ docker run -itd -p 80:80 -p 443:443 -v `pwd`/nginx_config:/etc/nginx/conf.d ng **promise-mysql**: 操作MySQL数据库依赖  Synopsis: Promise Mysql. - From: https://github.com/lukeb-uk/node-promise-mysql + From: https://github.com/lukeb-uk/node-promise-mysql **sequelize**: 关系型数据库ORM  Synopsis: Sequelize is a promise-based ORM for Node.js. @@ -244,31 +246,32 @@ $ docker run -itd -p 80:80 -p 443:443 -v `pwd`/nginx_config:/etc/nginx/conf.d ng  Synopsis: A pure node.js JavaScript Client implementing the MySql protocol.  From: https://github.com/mysqljs/mysql -支持Koa2的中间件列表:https://github.com/koajs/koa/wiki +支持Koa2的中间件列表:https://github.com/koajs/koa/wiki -**其它经常配合Koa2的插件:** +**其它经常配合Koa2的插件:** **koa-nunjucks-2**: 一个好用的模版引擎,可用于前后端,nunjucks:https://github.com/mozilla/nunjucks **koa-favicon**: -Koa的favicon中间件:https://github.com/koajs/favicon +Koa的favicon中间件:https://github.com/koajs/favicon **koa-server-push**: -HTTP2推送中间件:https://github.com/silenceisgolden/koa-server-push +HTTP2推送中间件:https://github.com/silenceisgolden/koa-server-push **koa-convert**: 转换旧的中间件支持Koa2  Synopsis: Convert koa generator-based middleware to promise-based middleware. - From: https://github.com/koajs/convert + From: https://github.com/koajs/convert **koa-logger**: 请求日志输出,需要配合上面的插件使用  Synopsis: Development style logger middleware for Koa.  From: https://github.com/koajs/logger **koa-onerror**: -Koa的错误拦截中间件,需要配合上面的插件使用:https://github.com/koajs/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或GET,true(异步)或 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']”。 diff --git a/src/app.js b/src/app.js index e7fb9da..0e008c0 100644 --- a/src/app.js +++ b/src/app.js @@ -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' diff --git a/config/main.js b/src/config.js similarity index 100% rename from config/main.js rename to src/config.js diff --git a/src/lib/mysql.js b/src/lib/mysql.js index 9df134d..1df3560 100644 --- a/src/lib/mysql.js +++ b/src/lib/mysql.js @@ -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({ diff --git a/src/lib/sequelize.js b/src/lib/sequelize.js index a48c867..41f80f9 100644 --- a/src/lib/sequelize.js +++ b/src/lib/sequelize.js @@ -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, diff --git a/src/plugins/smtp_sendemail/index.js b/src/plugins/smtp_sendemail/index.js index 4cb0957..931966b 100644 --- a/src/plugins/smtp_sendemail/index.js +++ b/src/plugins/smtp_sendemail/index.js @@ -1,5 +1,5 @@ import nodemailer from 'nodemailer' -import { SendEmail } from '../../../config/main' +import { SendEmail } from '../../config' // 发送Email(目前使用的是阿里云SMTP发送邮件) // receivers 目标邮箱,可以用英文逗号分隔多个。(我没试过) diff --git a/src/tool/Common.js b/src/tool/Common.js index 3b0b345..4bd9648 100644 --- a/src/tool/Common.js +++ b/src/tool/Common.js @@ -1,6 +1,6 @@ import { SystemConfig -} from '../config/main.js' +} from '../config' // 截取字符串,多余的部分用...代替 export let setString = (str, len) => {