Vue SSR Koa2 Scaffold
This commit is contained in:
24
config/koa/dev.js
Normal file
24
config/koa/dev.js
Normal file
@ -0,0 +1,24 @@
|
||||
const devMiddleware = require('webpack-dev-middleware')
|
||||
|
||||
module.exports = (compiler, opts) => {
|
||||
const expressMiddleware = devMiddleware(compiler, opts)
|
||||
|
||||
async function middleware (ctx, next) {
|
||||
await expressMiddleware(ctx.req, {
|
||||
end: (content) => {
|
||||
ctx.body = content
|
||||
},
|
||||
setHeader: (name, value) => {
|
||||
ctx.set(name, value)
|
||||
}
|
||||
}, next)
|
||||
}
|
||||
|
||||
middleware.getFilenameFromUrl = expressMiddleware.getFilenameFromUrl
|
||||
middleware.waitUntilValid = expressMiddleware.waitUntilValid
|
||||
middleware.invalidate = expressMiddleware.invalidate
|
||||
middleware.close = expressMiddleware.close
|
||||
middleware.fileSystem = expressMiddleware.fileSystem
|
||||
|
||||
return middleware
|
||||
}
|
31
config/koa/hot.js
Normal file
31
config/koa/hot.js
Normal file
@ -0,0 +1,31 @@
|
||||
// 参考自 https://github.com/ccqgithub/koa-webpack-hot/blob/master/index.js
|
||||
const hotMiddleware = require('webpack-hot-middleware')
|
||||
const PassThrough = require('stream').PassThrough
|
||||
|
||||
module.exports = (compiler, opts = {}) => {
|
||||
opts.path = opts.path || '/__webpack_hmr'
|
||||
|
||||
const middleware = hotMiddleware(compiler, opts)
|
||||
|
||||
return async (ctx, next) => {
|
||||
if (ctx.request.path !== opts.path) {
|
||||
return next()
|
||||
}
|
||||
|
||||
const stream = new PassThrough()
|
||||
ctx.body = stream
|
||||
|
||||
middleware(ctx.req, {
|
||||
write: stream.write.bind(stream),
|
||||
writeHead: (status, headers) => {
|
||||
ctx.status = status
|
||||
Object.keys(headers).forEach(key => {
|
||||
ctx.set(key, headers[key])
|
||||
})
|
||||
},
|
||||
end: () => {
|
||||
stream.end()
|
||||
}
|
||||
}, next)
|
||||
}
|
||||
}
|
74
config/koa/static.js
Normal file
74
config/koa/static.js
Normal file
@ -0,0 +1,74 @@
|
||||
'use strict'
|
||||
|
||||
/**
|
||||
* From koa-static
|
||||
*/
|
||||
|
||||
const { resolve } = require('path')
|
||||
const assert = require('assert')
|
||||
const send = require('koa-send')
|
||||
|
||||
/**
|
||||
* Expose `serve()`.
|
||||
*/
|
||||
|
||||
module.exports = serve
|
||||
|
||||
/**
|
||||
* Serve static files from `root`.
|
||||
*
|
||||
* @param {String} root
|
||||
* @param {Object} [opts]
|
||||
* @return {Function}
|
||||
* @api public
|
||||
*/
|
||||
|
||||
function serve (root, opts) {
|
||||
opts = Object.assign({}, opts)
|
||||
|
||||
assert(root, 'root directory is required to serve files')
|
||||
|
||||
// options
|
||||
opts.root = resolve(root)
|
||||
if (opts.index !== false) opts.index = opts.index || 'index.html'
|
||||
|
||||
if (!opts.defer) {
|
||||
return async function serve (ctx, next) {
|
||||
let done = false
|
||||
|
||||
if (ctx.method === 'HEAD' || ctx.method === 'GET') {
|
||||
if (ctx.path === '/' || ctx.path === '/index.html') { // exclude index.html file
|
||||
await next()
|
||||
return
|
||||
}
|
||||
try {
|
||||
done = await send(ctx, ctx.path, opts)
|
||||
} catch (err) {
|
||||
if (err.status !== 404) {
|
||||
throw err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!done) {
|
||||
await next()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return async function serve (ctx, next) {
|
||||
await next()
|
||||
|
||||
if (ctx.method !== 'HEAD' && ctx.method !== 'GET') return
|
||||
// response is already handled
|
||||
if (ctx.body != null || ctx.status !== 404) return // eslint-disable-line
|
||||
|
||||
try {
|
||||
await send(ctx, ctx.path, opts)
|
||||
} catch (err) {
|
||||
if (err.status !== 404) {
|
||||
throw err
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user