Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export default function middleware() {
return convert.compose(
logger(),
bodyParser(),
compress({
filter: function (content_type) {
if (/event-stream/i.test(content_type)) {
// 为了让hot reload生效,不对__webpack_hmr压缩
return false;
} else {
return true;
}
},
})
);
}
import cros from './middleware/crosMiddleware'
import pipeMiddleware from './middleware/pipeMiddleware'
import routing from './routes/'
import { port, mongodb } from './config'
mongoose.connect(mongodb)
mongoose.connection.on('error', console.error)
const app = new Koa()
app.jsonSpaces = 0 // 压缩json返回中的空格
app.keys = ['key']
app.use(convert.compose(
cros, // 跨域
logger(),
bodyParser(),
session(app),
pipeMiddleware()
))
// app.use(cros) // 跨域
// app.use(convert(logger()))
// app.use(convert(bodyParser()))
// app.use(convert(session(app)))
// app.use(pipeMiddleware())
routing(app)
app.listen(port, () => console.log(`✅ The server is running at http://localhost:${port}/`))
this.config.compress === false
? this.config.compress
: true
})
)
}
}
// use provided middleware
if (this.config.middleware)
for (
_b = 0, _c = this.config.middleware;
_b < _c.length;
_b++
) {
m = _c[_b]
this.app.use(KoaConvert.compose(m))
}
// use provided routers
if (this.config.routers)
for (_d = 0, _e = this.config.routers; _d < _e.length; _d++) {
r = _e[_d]
this.app.use(r.routes()).use(r.allowedMethods())
}
// 404 => SPA?
if (this.config.spaFileRelativePath) {
this.app.use(function(ctx) {
return __awaiter(_this, void 0, void 0, function() {
return __generator(this, function(_a) {
switch (_a.label) {
case 0:
if (!(ctx.status === 404)) return [3 /*break*/, 2]
return [
})
app.use(errorMiddleware())
app.use(bodyparser())
app.use(json())
app.use(httpMiddleware())
app.use(authMiddleware())
app.use(jsonSendMiddleware())
app.use(koaMount(
'/static', koaStatic(resolve(__dirname, '..', IS_DEV ? 'client/static' : 'client/dist/static')))
)
app.use(convert.compose(routes))
app.use(convert.compose(allowedMethods))
app.context.render = views(resolve(__dirname, '..', IS_DEV ? 'client' : 'client/dist'), {
map: { html: 'swig' }
})
app.use(historyApiFallback({
verbose: false,
whiteList: ['/api'],
rewrites: [
{
// connect-history-api-fallback 默认会对 url 中有 . 的 url 当成静态资源处理而不是当成页面地址来处理
// from: /\d+\.\d+\.\d+\.\d+$/,
from: /\/(\d+\.)*\d+$/,
to: '/'
},
{
// 请求解析
app.use(
convert(
koaBody({
multipart: true,
formLimit: '5mb',
formidable: {
uploadDir: path.join(__dirname, '../tmp'),
},
}),
),
)
// middleware
app.use(
convert.compose(
stateMiddle(),
authority(route.authRoutes), // 权限验证
validation(), // 验证参数
pipe(), // 通讯
),
)
// 静态文件
app.use(convert(koaStatic(path.join(__dirname, '../static'))))
// 渲染引擎
// app.use(views(path.join(__dirname, './static/theme/'), {
// extension: 'ejs',
// }))
// 网络日志
)
}
if (this.config.json !== false) {
this.app.use(
KoaJson({
pretty: this.config.jsonPretty || this.env === 'development',
param: this.config.jsonPrettyParam || undefined,
spaces: this.config.jsonSpaces || 2
})
)
}
// use provided middleware
if (this.config.middleware)
for (const m of this.config.middleware)
this.app.use(KoaConvert.compose(m) as Koa.Middleware)
// use provided routers
if (this.config.routers)
for (const r of this.config.routers)
this.app.use(r.routes()).use(r.allowedMethods())
// use provided public directories (with static cache)
if (this.config.publicDirs) {
for (const dir of this.config.publicDirs) {
if (!this.config.cacheFiles) this.config.cacheFiles = {}
const cacheFiles = { ...this.config.cacheFiles }
for (const pathKey in cacheFiles) {
if (!cacheFiles.hasOwnProperty(pathKey)) continue
const value = cacheFiles[pathKey]
delete cacheFiles[pathKey]
// normalize paths
logger.error(err)
})
app.use(errorMiddleware())
app.use(bodyparser())
app.use(json())
app.use(httpMiddleware())
app.use(authMiddleware())
app.use(jsonSendMiddleware())
app.use(koaMount(
'/static', koaStatic(resolve(__dirname, '..', IS_DEV ? 'client/static' : 'client/dist/static')))
)
app.use(convert.compose(routes))
app.use(convert.compose(allowedMethods))
app.context.render = views(resolve(__dirname, '..', IS_DEV ? 'client' : 'client/dist'), {
map: { html: 'swig' }
})
app.use(historyApiFallback({
verbose: false,
whiteList: ['/api'],
rewrites: [
{
// connect-history-api-fallback 默认会对 url 中有 . 的 url 当成静态资源处理而不是当成页面地址来处理
// from: /\d+\.\d+\.\d+\.\d+$/,
from: /\/(\d+\.)*\d+$/,
to: '/'
},
)
}
if (this.config.json !== false) {
this.app.use(
KoaJson({
pretty: this.config.jsonPretty || this.env === 'development',
param: this.config.jsonPrettyParam || undefined,
spaces: this.config.jsonSpaces || 2
})
)
}
// use provided middleware
if (this.config.middleware)
for (_i = 0, _a = this.config.middleware; _i < _a.length; _i++) {
m = _a[_i]
this.app.use(KoaConvert.compose(m))
}
// use provided routers
if (this.config.routers)
for (_b = 0, _c = this.config.routers; _b < _c.length; _b++) {
r = _c[_b]
this.app.use(r.routes()).use(r.allowedMethods())
}
// use provided public directories (with static cache)
if (this.config.publicDirs) {
_loop_1 = function(dir) {
if (!this_1.config.cacheFiles) this_1.config.cacheFiles = {}
var cacheFiles = __assign({}, this_1.config.cacheFiles)
for (var pathKey in cacheFiles) {
if (!cacheFiles.hasOwnProperty(pathKey)) continue
var value = cacheFiles[pathKey]
delete cacheFiles[pathKey]
export default function api() {
const router = new Router({
prefix: config.app.baseApi,
});
Object.keys(routes).forEach(name => {
return routes[name]['default'](router);
});
return convert.compose([
router.routes(),
router.allowedMethods(),
]);
}