forked from chenshenhai/koa2-note
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 9bbb816
Showing
171 changed files
with
6,165 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
node_modules/ | ||
npm-debug.log | ||
_book/ | ||
demo/*/node_modules | ||
demo/*/npm-debug.log | ||
demo/*/upload-files/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# hello world | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# Summary | ||
|
||
* [koa2开始]() | ||
* [快速开始](note/start/quick.md) | ||
* [async/await使用](note/start/async.md) | ||
* [koa2简析结构](note/start/info.md) | ||
* [koa中间件开发与使用](note/start/middleware.md) | ||
* [路由]() | ||
* [原生koa2实现路由](note/route/simple.md) | ||
* [koa-router中间件](note/route/koa-router.md) | ||
* [请求数据获取]() | ||
* [GET请求数据获取](note/request/get.md) | ||
* [POST请求数据获取](note/request/post.md) | ||
* [koa-bodyparser中间件](note/request/post-use-middleware.md) | ||
* [静态资源加载]() | ||
* [原生koa2实现静态资源服务器](note/static/server.md) | ||
* [koa-static中间件](note/static/middleware.md) | ||
* [cookie/session]() | ||
* [koa2使用cookie](note/cookie/info.md) | ||
* [koa2实现session](note/session/info.md) | ||
* [模板引擎]() | ||
* [koa2加载模板引擎](note/template/add.md) | ||
* [ejs模板引擎](note/template/ejs.md) | ||
* [文件上传]() | ||
* [busboy模块](note/upload/busboy.md) | ||
* [上传文件简单实现](note/upload/simple.md) | ||
* [数据库mysql]() | ||
* [mysql模块](note/mysql/info.md) | ||
* [async/await封装使用mysql](note/mysql/async.md) | ||
* [项目建表初始化](note/mysql/init.md) | ||
* [项目框架搭建]() | ||
* [框架设计](note/project/framework.md) | ||
* [分层操作](note/project/layer.md) | ||
* [数据库设计](note/project/sql.md) | ||
* [路由设计](note/project/route.md) | ||
* [webpack2环境搭建](note/project/webpack2.md) | ||
* [使用react.js](note/project/react.md) | ||
* [登录注册功能实现](note/project/sign.md) | ||
* [session登录态判断处理](note/project/session.md) | ||
* [debug]() | ||
* [开发debug](README.md) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
const Koa = require('koa') | ||
const app = new Koa() | ||
|
||
app.use( async ( ctx ) => { | ||
|
||
if ( ctx.url === '/index' ) { | ||
// 设置cookie | ||
ctx.cookies.set( | ||
'cid', | ||
'hello world', | ||
{ | ||
domain: 'localhost', // 写cookie所在的域名 | ||
path: '/index', // 写cookie所在的路径 | ||
maxAge: 10 * 60 * 1000, // cookie有效时长 | ||
expires: new Date('2017-02-15'), // cookie失效时间 | ||
httpOnly: false, // 是否只用于http请求中获取 | ||
overwrite: false // 是否允许重写 | ||
} | ||
) | ||
ctx.body = 'cookie is ok' | ||
} else { | ||
ctx.body = 'hello world' | ||
} | ||
|
||
}) | ||
|
||
app.listen(3000) | ||
console.log('[demo] cookie is starting at port 3000') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"name": "cookie", | ||
"version": "1.0.0", | ||
"description": "koa cookie demo", | ||
"main": "index.js", | ||
"scripts": { | ||
"start": "node --harmony index.js" | ||
}, | ||
"keywords": [ | ||
"koajs" | ||
], | ||
"author": "chenshenhai", | ||
"license": "MIT", | ||
"dependencies": { | ||
"koa": "^2.0.0-alpha.8" | ||
}, | ||
"engines": { | ||
"node": ">=7.0.0", | ||
"npm": "~3.0.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
const Koa = require('koa') | ||
const views = require('koa-views') | ||
const path = require('path') | ||
const app = new Koa() | ||
|
||
app.use(views(path.join(__dirname, './view'), { | ||
extension: 'ejs' | ||
})) | ||
|
||
app.use( async ( ctx ) => { | ||
let title = 'hello koa2' | ||
await ctx.render('index', { | ||
title, | ||
}) | ||
}) | ||
|
||
app.listen(3000) | ||
console.log('[demo] ejs is starting at port 3000') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"name": "ejs", | ||
"version": "1.0.0", | ||
"description": "koa ejs demo", | ||
"main": "index.js", | ||
"scripts": { | ||
"start": "node --harmony index.js" | ||
}, | ||
"keywords": [ | ||
"koajs" | ||
], | ||
"author": "chenshenhai", | ||
"license": "MIT", | ||
"dependencies": { | ||
"koa": "^2.0.0-alpha.8" | ||
}, | ||
"engines": { | ||
"node": ">=7.0.0", | ||
"npm": "~3.0.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title><%= title %></title> | ||
</head> | ||
<body> | ||
<h1><%= title %></h1> | ||
<p>EJS Welcome to <%= title %></p> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
|
||
const fs = require('fs'); | ||
const getSqlContentMap = require('./util/get-sql-content-map'); | ||
const { query } = require('./util/db'); | ||
|
||
|
||
// 打印脚本执行日志 | ||
const eventLog = function( err , sqlFile, index ) { | ||
if( err ) { | ||
console.log(`[ERROR] sql脚本文件: ${sqlFile} 第${index + 1}条脚本 执行失败 o(╯□╰)o !`) | ||
} else { | ||
console.log(`[SUCCESS] sql脚本文件: ${sqlFile} 第${index + 1}条脚本 执行成功 O(∩_∩)O !`) | ||
} | ||
} | ||
|
||
// 获取所有sql脚本内容 | ||
let sqlContentMap = getSqlContentMap() | ||
|
||
// 执行建表sql脚本 | ||
const createAllTables = async () => { | ||
for( let key in sqlContentMap ) { | ||
let sqlShell = sqlContentMap[key] | ||
let sqlShellList = sqlShell.split(';') | ||
|
||
for ( let [ i, shell ] of sqlShellList.entries() ) { | ||
if ( shell.trim() ) { | ||
let result = await query( shell ) | ||
if ( result.serverStatus * 1 === 2 ) { | ||
eventLog( null, key, i) | ||
} else { | ||
eventLog( true, key, i) | ||
} | ||
} | ||
} | ||
} | ||
console.log('sql脚本执行结束!') | ||
console.log('请按 ctrl + c 键退出!') | ||
|
||
} | ||
|
||
createAllTables(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"name": "mysql", | ||
"version": "1.0.0", | ||
"description": "koa start demo", | ||
"main": "index.js", | ||
"scripts": { | ||
"start": "node --harmony index.js" | ||
}, | ||
"keywords": [ | ||
"koajs" | ||
], | ||
"author": "chenshenhai", | ||
"license": "MIT", | ||
"dependencies": { | ||
"koa": "^2.0.0-alpha.8", | ||
"mysql": "^2.13.0" | ||
}, | ||
"engines": { | ||
"node": ">=7.0.0", | ||
"npm": "~3.0.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
CREATE TABLE IF NOT EXISTS `data` ( | ||
`id` int(11) NOT NULL AUTO_INCREMENT, | ||
`data_info` json DEFAULT NULL, | ||
`create_time` varchar(20) DEFAULT NULL, | ||
`modified_time` varchar(20) DEFAULT NULL, | ||
`level` int(11) DEFAULT NULL, | ||
PRIMARY KEY (`id`) | ||
) ENGINE=InnoDB DEFAULT CHARSET=utf8 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
CREATE TABLE IF NOT EXISTS `user` ( | ||
`id` int(11) NOT NULL AUTO_INCREMENT, | ||
`email` varchar(255) DEFAULT NULL, | ||
`password` varchar(255) DEFAULT NULL, | ||
`name` varchar(255) DEFAULT NULL, | ||
`nick` varchar(255) DEFAULT NULL, | ||
`detail_info` json DEFAULT NULL, | ||
`create_time` varchar(20) DEFAULT NULL, | ||
`modified_time` varchar(20) DEFAULT NULL, | ||
`level` int(11) DEFAULT NULL, | ||
PRIMARY KEY (`id`) | ||
) ENGINE=InnoDB DEFAULT CHARSET=utf8; | ||
|
||
INSERT INTO `user` set email='1@example.com', password='123456'; | ||
INSERT INTO `user` set email='2@example.com', password='123456'; | ||
INSERT INTO `user` set email='3@example.com', password='123456'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
const mysql = require('mysql') | ||
|
||
const pool = mysql.createPool({ | ||
host : '127.0.0.1', | ||
user : 'root', | ||
password : 'abc123', | ||
database : 'koa_demo' | ||
}) | ||
|
||
let query = function( sql, values ) { | ||
|
||
return new Promise(( resolve, reject ) => { | ||
pool.getConnection(function(err, connection) { | ||
if (err) { | ||
resolve( err ) | ||
} else { | ||
connection.query(sql, values, ( err, rows) => { | ||
|
||
if ( err ) { | ||
reject( err ) | ||
} else { | ||
resolve( rows ) | ||
} | ||
connection.release() | ||
}) | ||
} | ||
}) | ||
}) | ||
|
||
} | ||
|
||
|
||
module.exports = { | ||
query | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
const fs = require('fs') | ||
const getSqlMap = require('./get-sql-map') | ||
|
||
let sqlContentMap = {} | ||
|
||
/** | ||
* 读取sql文件内容 | ||
* @param {string} fileName 文件名称 | ||
* @param {string} path 文件所在的路径 | ||
* @return {string} 脚本文件内容 | ||
*/ | ||
function getSqlContent( fileName, path ) { | ||
let content = fs.readFileSync( path, 'binary' ) | ||
sqlContentMap[ fileName ] = content | ||
} | ||
|
||
/** | ||
* 封装所有sql文件脚本内容 | ||
* @return {object} | ||
*/ | ||
function getSqlContentMap () { | ||
let sqlMap = getSqlMap() | ||
for( let key in sqlMap ) { | ||
getSqlContent( key, sqlMap[key] ) | ||
} | ||
|
||
return sqlContentMap | ||
} | ||
|
||
module.exports = getSqlContentMap |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
const fs = require('fs') | ||
const walkFile = require('./walk-file') | ||
|
||
/** | ||
* 获取sql目录下的文件目录数据 | ||
* @return {object} | ||
*/ | ||
function getSqlMap () { | ||
let basePath = __dirname | ||
basePath = basePath.replace(/\\/g, '\/') | ||
|
||
let pathArr = basePath.split('\/') | ||
pathArr = pathArr.splice( 0, pathArr.length - 1 ) | ||
basePath = pathArr.join('/') + '/sql/' | ||
|
||
let fileList = walkFile( basePath, 'sql' ) | ||
return fileList | ||
} | ||
|
||
module.exports = getSqlMap |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
const fs = require('fs') | ||
|
||
/** | ||
* 遍历目录下的文件目录 | ||
* @param {string} pathResolve 需进行遍历的目录路径 | ||
* @param {string} mime 遍历文件的后缀名 | ||
* @return {object} 返回遍历后的目录结果 | ||
*/ | ||
const walkFile = function( pathResolve , mime ){ | ||
|
||
let files = fs.readdirSync( pathResolve ) | ||
|
||
let fileList = {} | ||
|
||
for( let [ i, item] of files.entries() ) { | ||
let itemArr = item.split('\.') | ||
|
||
let itemMime = ( itemArr.length > 1 ) ? itemArr[ itemArr.length - 1 ] : 'undefined' | ||
let keyName = item + '' | ||
if( mime === itemMime ) { | ||
fileList[ item ] = pathResolve + item | ||
} | ||
} | ||
|
||
return fileList | ||
} | ||
|
||
module.exports = walkFile |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"presets": [["es2015", { "modules": false }], "react", "stage-2"], | ||
"plugins": [ | ||
"transform-runtime", | ||
["import", { "libraryName": "antd", "style": "css" }] | ||
] | ||
} |
Oops, something went wrong.