-
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 2a308fe
Showing
17 changed files
with
1,887 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,112 @@ | ||
|
||
# Logs | ||
logs | ||
*.log | ||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
lerna-debug.log* | ||
|
||
# Diagnostic reports (https://nodejs.org/api/report.html) | ||
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json | ||
|
||
# Runtime data | ||
pids | ||
*.pid | ||
*.seed | ||
*.pid.lock | ||
|
||
# Directory for instrumented libs generated by jscoverage/JSCover | ||
lib-cov | ||
|
||
# Coverage directory used by tools like istanbul | ||
coverage | ||
*.lcov | ||
|
||
# nyc test coverage | ||
.nyc_output | ||
|
||
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) | ||
.grunt | ||
|
||
# Bower dependency directory (https://bower.io/) | ||
bower_components | ||
|
||
# node-waf configuration | ||
.lock-wscript | ||
|
||
# Compiled binary addons (https://nodejs.org/api/addons.html) | ||
build/Release | ||
|
||
# Dependency directories | ||
node_modules/ | ||
jspm_packages/ | ||
|
||
# TypeScript v1 declaration files | ||
typings/ | ||
|
||
# TypeScript cache | ||
*.tsbuildinfo | ||
|
||
# Optional npm cache directory | ||
.npm | ||
|
||
# Optional eslint cache | ||
.eslintcache | ||
|
||
# Microbundle cache | ||
.rpt2_cache/ | ||
.rts2_cache_cjs/ | ||
.rts2_cache_es/ | ||
.rts2_cache_umd/ | ||
|
||
# Optional REPL history | ||
.node_repl_history | ||
|
||
# Output of 'npm pack' | ||
*.tgz | ||
|
||
# Yarn Integrity file | ||
.yarn-integrity | ||
|
||
# dotenv environment variables file | ||
.env | ||
.env.test | ||
|
||
# parcel-bundler cache (https://parceljs.org/) | ||
.cache | ||
|
||
# Next.js build output | ||
.next | ||
|
||
# Nuxt.js build / generate output | ||
.nuxt | ||
dist | ||
|
||
# Gatsby files | ||
.cache/ | ||
# Comment in the public line in if your project uses Gatsby and not Next.js | ||
# https://nextjs.org/blog/next-9-1#public-directory-support | ||
# public | ||
|
||
# vuepress build output | ||
.vuepress/dist | ||
|
||
# Serverless directories | ||
.serverless/ | ||
|
||
# FuseBox cache | ||
.fusebox/ | ||
|
||
# DynamoDB Local files | ||
.dynamodb/ | ||
|
||
# TernJS port file | ||
.tern-port | ||
|
||
# Stores VSCode versions used for testing VSCode extensions | ||
.vscode-test | ||
|
||
# user | ||
|
||
demo_kao2.sh |
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,63 @@ | ||
const mysql = require('mysql') | ||
const mysql_config = require('../config/config.json').mysql | ||
|
||
// connection.connect((err, result) => { | ||
// if (err) { | ||
// console.log(err); | ||
// console.log("连接失败"); | ||
// return; | ||
// } | ||
// console.log(result); | ||
// console.log("连接成功"); | ||
// }); | ||
let db_v1 = (sql, addSqlParams, callback) => { | ||
let connection = mysql.createConnection(mysql_config); | ||
connection.connect() | ||
|
||
connection.query(sql, addSqlParams, (err, data) => { | ||
console.dir(err); | ||
console.dir(data); | ||
callback && callback(err, data) | ||
}) | ||
connection.end(); // 结束连接 | ||
} | ||
let db = (sql, addSqlParams) => { | ||
return new Promise((resolve, reject) => { | ||
let connection = mysql.createConnection(mysql_config); | ||
connection.connect() | ||
connection.query(sql, addSqlParams, (err, data) => { | ||
if (err) { reject(err) } | ||
else { | ||
resolve(data) | ||
} | ||
}) | ||
connection.end(); // 结束连接 | ||
}) | ||
} | ||
|
||
|
||
let pool = mysql.createPool(mysql_config); | ||
|
||
let query = (sql, addSqlParams) => {//返回promise 对象 | ||
return new Promise((resolve, reject) => { | ||
pool.getConnection((err, con) => {// 连接 | ||
if (err) { | ||
reject(err) | ||
} else { | ||
con.query(sql, addSqlParams, (err, data) => {//执行 | ||
if (err) { | ||
reject(err) | ||
} else { | ||
resolve(data) | ||
} | ||
con.release() | ||
}) | ||
} | ||
}) | ||
}) | ||
} | ||
|
||
// module.exports = { db:db } 相当于 | ||
// module.exports = { db } | ||
|
||
module.exports = { db, query, db_v1 } |
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 @@ | ||
# Koa2 |
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,5 @@ | ||
const Koa = require('koa') | ||
const middleware = require('./middleware') | ||
const app = new Koa() | ||
middleware(app) | ||
app.listen(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,9 @@ | ||
{ | ||
"mysql": { | ||
"host": "", | ||
"user": "root", | ||
"password": "", | ||
"port": 3306, | ||
"database": "nodeTest" | ||
} | ||
} |
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,92 @@ | ||
const db = require('../Data/mysql_help') | ||
|
||
let index = async (ctx, next) => { | ||
let h1_txt = 'hello koa2' | ||
let span_txt = 'ejs 渲染' | ||
await ctx.render('index', { h1_txt, span_txt }) | ||
} | ||
|
||
let Get = async (ctx, next) => { | ||
let url = ctx.url | ||
// 从上下文的request对象中获取 | ||
let request = ctx.request | ||
let req_query = request.query | ||
let req_querystring = request.querystring | ||
|
||
// 从上下文中直接获取 | ||
let ctx_query = ctx.query | ||
let ctx_querystring = ctx.querystring | ||
|
||
ctx.body = { | ||
url, | ||
req_query, | ||
req_querystring, | ||
ctx_query, | ||
ctx_querystring | ||
} | ||
} | ||
|
||
let post = async (ctx, next) => { | ||
ctx.body = ctx.request.body // 获取post 参数 | ||
} | ||
|
||
let cookies = async (ctx, next) => { | ||
ctx.cookies.set( | ||
'cid', | ||
'hello world', | ||
{ | ||
domain: 'localhost', // 写cookie所在的域名 | ||
// path: '/index', // 写cookie所在的路径 | ||
maxAge: 1000 * 5, // cookie有效时长 | ||
// expires: new Date('2021-02-15'), // cookie失效时间 | ||
httpOnly: false, // 是否只用于http请求中获取 | ||
overwrite: false // 是否允许重写 | ||
} | ||
) | ||
ctx.body = 'cookie is ok' | ||
} | ||
|
||
let setsession = async (ctx, next) => { | ||
try { | ||
ctx.session.username = ctx.query.name | ||
ctx.session.age = ctx.query.age | ||
ctx.body = { status: 200, msg: 'session设置成功' }; | ||
} catch (e) { | ||
ctx.body = { status: 200, msg: 'session设置失败', err: e }; | ||
} | ||
} | ||
|
||
const Getsession = async (ctx, next) => { | ||
try { | ||
let name = ctx.session.username, | ||
age = ctx.session.age; | ||
ctx.body = { | ||
status: 200, | ||
msg: 'session获取成功', | ||
name: name, | ||
age: age | ||
}; | ||
} catch (e) { | ||
ctx.body = { | ||
status: 200, | ||
msg: 'session获取失败', | ||
err: e | ||
}; | ||
} | ||
} | ||
|
||
module.exports = { | ||
index, Get, post, cookies, setsession, Getsession, | ||
home: async (ctx, next) => { | ||
let h1_txt = 'hello koa2' | ||
let span_txt = 'ejs 渲染' | ||
await ctx.render('index', { h1_txt, span_txt }) | ||
}, | ||
GetData: async (ctx, next) => { | ||
ctx.body = await db.query('select * from city', '') | ||
}, | ||
Get_aid: async (ctx, next) => {// 动态路由传入多个参数 | ||
// console.dir(ctx); http://localhost:3000/demo/aid=12/bid=20 | ||
ctx.body = ctx.params // 获取动态路由传值 {"aid": "aid=12","bid": "bid=20"} | ||
} | ||
} |
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,83 @@ | ||
const router = require('../routers') //路由 | ||
const static = require('koa-static') // 静态资源处理 更多参照express_demo | ||
const session = require('koa-session') // session | ||
const staticCache = require('koa-static-cache') // 静态缓存 | ||
const auth = require('koa-basic-auth') // 身份验证中间件 | ||
const mount = require('koa-mount') // 指定路由添加权限 | ||
const log4js = require('./logger/index') // logs4js | ||
const views = require('koa-views') //模板引擎 中间件 要安装ejs | ||
const path = require('path') | ||
const bodyParser = require('koa-bodyparser')// 获取post 参数 | ||
|
||
//#region | ||
const GetPostData = (app) => { app.use(bodyParser()) } | ||
const Identity_verification = (app) => { | ||
app.use(async (ctx, next) => { | ||
try { | ||
await next(); | ||
} catch (err) { | ||
if (401 == err.status) { | ||
ctx.status = 401; | ||
ctx.set('WWW-Authenticate', 'Basic'); | ||
ctx.body = '401'; | ||
} else { | ||
throw err; | ||
} | ||
} | ||
}) | ||
// app.use(auth({ name: 'admin', pass: 'admin' })) //访问全局 | ||
app.use(mount('/images', auth({ name: 'root', pass: 'root' }))) // 访问特定路由 | ||
} | ||
|
||
const sessions = (app) => { | ||
app.keys = ['some secret hurr'] | ||
const config = { | ||
key: 'koa:sess', //cookie key (default is koa:sess) | ||
maxAge: 86400000, // cookie的过期时间 maxAge in ms (default is 1 days) | ||
overwrite: true, //是否可以overwrite (默认default true) | ||
httpOnly: true, //cookie是否只有服务器端可以访问 httpOnly or not (default true) | ||
signed: true, //签名默认true | ||
rolling: false, //在每次请求时强行设置cookie,这将重置cookie过期时间(默认:false) | ||
renew: false, //(boolean) renew session when session is nearly expired, | ||
} | ||
app.use(session(config, app)) | ||
} | ||
|
||
const Render_pages = (app) => { | ||
app.use(views(path.join(__dirname, '../view/'), { | ||
extension: 'ejs' | ||
})) //模板渲染要在 router 之前 | ||
} | ||
|
||
const Static_resource = (app) => { | ||
app.use(staticCache(path.join(__dirname, '../public/'), {// 静态资源缓存 | ||
maxAge: 365 * 24 * 60 * 60 | ||
})) | ||
app.use(static(path.join(__dirname, '../public/'))) // __dirname 当前路径 相当于 linux pwd 命令 | ||
// http://localhost:3000/images/bg.jpg | ||
} | ||
|
||
const logger = (app) => { | ||
app.use(log4js({ | ||
env: app.env, | ||
projectName: 'demo_koa2', | ||
appLogLevel: 'all', | ||
dir: 'logs', | ||
serverIp: '127.0.0.1' | ||
})) | ||
} | ||
|
||
const routers = (app) => { | ||
router(app) | ||
} | ||
//#endregion | ||
|
||
module.exports = { | ||
logger, | ||
Static_resource, | ||
Render_pages, | ||
sessions, | ||
Identity_verification, | ||
GetPostData, | ||
routers | ||
} |
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 @@ | ||
const all_middleware = require('./all_middleware')// 所有中间件 | ||
module.exports = (app) => { | ||
all_middleware.logger(app) | ||
all_middleware.Static_resource(app) | ||
all_middleware.GetPostData(app) | ||
all_middleware.Identity_verification(app) | ||
all_middleware.Render_pages(app) | ||
all_middleware.sessions(app) | ||
all_middleware.routers(app) | ||
} |
Oops, something went wrong.