mirror of
https://github.com/roleypoly/roleypoly-v1.git
synced 2025-04-24 19:59:12 +00:00
replace koa-send, it doesn't do the business right.
This commit is contained in:
parent
84ebabd749
commit
96d91caba0
3 changed files with 75 additions and 10 deletions
|
@ -5,6 +5,7 @@ const http = require('http')
|
|||
const Koa = require('koa')
|
||||
const app = new Koa()
|
||||
const _io = require('socket.io')
|
||||
const fs = require('fs')
|
||||
const path = require('path')
|
||||
const router = require('koa-better-router')().loadMethods()
|
||||
const Roleypoly = require('./Roleypoly')
|
||||
|
@ -50,17 +51,72 @@ async function start () {
|
|||
log.info('public path', pub)
|
||||
|
||||
const staticFiles = require('koa-static')
|
||||
// app.use(staticFiles(pub, { defer: true, gzip: true, br: true }))
|
||||
|
||||
// const send = require('koa-send')
|
||||
const send = require('koa-send')
|
||||
app.use(async (ctx, next) => {
|
||||
if (ctx.path.startsWith('/api')) {
|
||||
log.info('api breakout')
|
||||
return next()
|
||||
}
|
||||
|
||||
const chkPath = path.resolve(path.join(pub, ctx.path))
|
||||
log.info('chkPath', chkPath)
|
||||
if (!chkPath.startsWith(pub)) {
|
||||
return next()
|
||||
}
|
||||
|
||||
try {
|
||||
fs.statSync(chkPath)
|
||||
log.info('sync pass')
|
||||
ctx.body = fs.readFileSync(chkPath, { encoding: 'utf-8' })
|
||||
ctx.type = path.extname(ctx.path)
|
||||
log.info('body sent')
|
||||
ctx.status = 200
|
||||
return next()
|
||||
} catch (e) {
|
||||
log.warn('failed')
|
||||
if (ctx.path.startsWith('/static/')) {
|
||||
return next()
|
||||
}
|
||||
try {
|
||||
ctx.body = fs.readFileSync(path.join(pub, 'index.html'), { encoding: 'utf-8' })
|
||||
} catch (e) {
|
||||
ctx.body = e.stack || e.trace
|
||||
ctx.status = 500
|
||||
}
|
||||
log.info('index sent')
|
||||
ctx.status = 200
|
||||
return next()
|
||||
}
|
||||
|
||||
// try {
|
||||
// await next()
|
||||
// } catch (e) {
|
||||
// send(ctx, 'index.html', { root: pub })
|
||||
// }
|
||||
|
||||
})
|
||||
// const sendOpts = {root: pub, index: 'index.html'}
|
||||
// // const sendOpts = {}
|
||||
// app.use(async (ctx, next) => {
|
||||
// if (ctx.path.startsWith('/api')) {
|
||||
// return next()
|
||||
// return await next()
|
||||
// }
|
||||
|
||||
// // await next()
|
||||
// // send(ctx, 'index.html', { root: pub })
|
||||
// try {
|
||||
// log.info('pass 1', ctx.path, __dirname+'/public'+ctx.path)
|
||||
// await send(ctx, __dirname+'/public'+ctx.path, sendOpts)
|
||||
// } catch (error) {
|
||||
// try {
|
||||
// log.info('pass 2 /', ctx.path, __dirname+'/public/index.html')
|
||||
// await send(ctx, __dirname+'/public/index.html', sendOpts)
|
||||
// } catch (error) {
|
||||
// log.info('exit to next', ctx.path)
|
||||
// await next()
|
||||
// }
|
||||
// }
|
||||
// })
|
||||
app.use(staticFiles(pub, { defer: true, gzip: true, br: true }))
|
||||
}
|
||||
|
||||
// Request logger
|
||||
|
@ -82,7 +138,7 @@ async function start () {
|
|||
let timeElapsed = new Date() - timeStart
|
||||
|
||||
log.request(`${ctx.status} ${ctx.method} ${ctx.url} - ${ctx.ip} - took ${timeElapsed}ms`)
|
||||
return null
|
||||
// return null
|
||||
})
|
||||
|
||||
const session = require('koa-session')
|
||||
|
|
|
@ -22,7 +22,7 @@
|
|||
"koa-better-router": "^2.1.1",
|
||||
"koa-bodyparser": "^4.2.1",
|
||||
"koa-compress": "^3.0.0",
|
||||
"koa-send": "^5.0.0",
|
||||
"koa-send": "latest-2",
|
||||
"koa-session": "^5.10.0",
|
||||
"koa-static": "^5.0.0",
|
||||
"ksuid": "^1.1.3",
|
||||
|
|
|
@ -656,7 +656,7 @@ debug@^2.1.2, debug@^2.2.0, debug@^2.3.3, debug@^2.6.3, debug@^2.6.9:
|
|||
dependencies:
|
||||
ms "2.0.0"
|
||||
|
||||
debug@^3, debug@^3.0, debug@^3.1.0:
|
||||
debug@^3, debug@^3.0, debug@^3.1.0, debug@^3.2.5:
|
||||
version "3.2.6"
|
||||
resolved "https://registry.yarnpkg.com/debug/-/debug-3.2.6.tgz#e83d17de16d8a7efb7717edbe5fb10135eee629b"
|
||||
integrity sha512-mel+jf7nrtEl5Pn1Qx46zARXKDpBbvzezse7p7LqINmdoIk8PYP5SySaxEmYv6TZ0JyEKA1hsCId6DIhgITtWQ==
|
||||
|
@ -1841,6 +1841,15 @@ koa-send@^5.0.0:
|
|||
mz "^2.7.0"
|
||||
resolve-path "^1.4.0"
|
||||
|
||||
koa-send@latest-2:
|
||||
version "2.0.3"
|
||||
resolved "https://registry.yarnpkg.com/koa-send/-/koa-send-2.0.3.tgz#7fd3cf3ce1418bade376a84cea4ec31d8f49cf07"
|
||||
integrity sha512-fX69m+IqgXT4ZgBMsGVsMMB5tLYs9Sa1MhQLyau0n5Qetq5003aaFoCY/YpPL4M+WA7fpJfFk6aEArNzGP0Q3w==
|
||||
dependencies:
|
||||
debug "^3.2.5"
|
||||
mz "^2.0.0"
|
||||
resolve-path "^1.2.1"
|
||||
|
||||
koa-session@^5.10.0:
|
||||
version "5.10.0"
|
||||
resolved "https://registry.yarnpkg.com/koa-session/-/koa-session-5.10.0.tgz#223a1d0d16fc4315c48fda7cdd08113b0c7a6518"
|
||||
|
@ -2093,7 +2102,7 @@ mute-stream@0.0.7, mute-stream@~0.0.4:
|
|||
resolved "https://registry.yarnpkg.com/mute-stream/-/mute-stream-0.0.7.tgz#3075ce93bc21b8fab43e1bc4da7e8115ed1e7bab"
|
||||
integrity sha1-MHXOk7whuPq0PhvE2n6BFe0ee6s=
|
||||
|
||||
mz@^2.7.0:
|
||||
mz@^2.0.0, mz@^2.7.0:
|
||||
version "2.7.0"
|
||||
resolved "https://registry.yarnpkg.com/mz/-/mz-2.7.0.tgz#95008057a56cafadc2bc63dde7f9ff6955948e32"
|
||||
integrity sha512-z81GNO7nnYMEhrGh9LeymoE4+Yr0Wn5McHIZMK5cfQCl+NDX08sCZgUc9/6MHni9IWuFLm1Z3HTCXu2z9fN62Q==
|
||||
|
@ -2716,7 +2725,7 @@ resolve-from@^1.0.0:
|
|||
resolved "https://registry.yarnpkg.com/resolve-from/-/resolve-from-1.0.1.tgz#26cbfe935d1aeeeabb29bc3fe5aeb01e93d44226"
|
||||
integrity sha1-Jsv+k10a7uq7Kbw/5a6wHpPUQiY=
|
||||
|
||||
resolve-path@^1.4.0:
|
||||
resolve-path@^1.2.1, resolve-path@^1.4.0:
|
||||
version "1.4.0"
|
||||
resolved "https://registry.yarnpkg.com/resolve-path/-/resolve-path-1.4.0.tgz#c4bda9f5efb2fce65247873ab36bb4d834fe16f7"
|
||||
integrity sha1-xL2p9e+y/OZSR4c6s2u02DT+Fvc=
|
||||
|
|
Loading…
Add table
Reference in a new issue