1 Star 0 Fork 0

liushuai/webfunny_monitor

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
app.js 1.62 KB
一键复制 编辑 原始数据 按行查看 历史
jiangyw 提交于 2020-10-21 01:34 . 小程序Beta版本发布
const Koa = require('koa')
// 路由
const bodyParser = require('koa-bodyparser')
const httpRoute = require('./routes')
const wsRoute = require('./routes/ws')
const log = require("./config/log")
let WebSocket = require("koa-websocket")
const statusCode = require('./util/status-code')
const err = require('./middlreware/error')
const app = WebSocket(new Koa())
app.use(async (ctx, next) => {
ctx.set("Access-Control-Allow-Origin", "*")
ctx.set("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS")
ctx.set("Access-Control-Allow-Headers", "*")
ctx.set("Access-Control-Allow-Credentials", true)
ctx.set("X-Powered-By", "3.2.1")
ctx.set("Content-Type", "application/json;charset=utf-8")
ctx.set("Connection", "close")
if (ctx.method == 'OPTIONS') {
ctx.body = 200;
} else {
await next();
}
})
// 登录校验
app.use(err())
// middlewares
app.use(bodyParser({
enableTypes: ['json', 'form', 'text'],
formLimit: "5mb",
jsonLimit: "5mb",
textLimit: "5mb"
}))
app.use(async (ctx, next) => {
const start = new Date()
let ms = 0
try {
await next();
ms = new Date() - start
} catch (error) {
//记录异常日志
log.error(ctx, error, ms);
ctx.response.status = 500;
ctx.body = statusCode.ERROR_500('服务器异常,请检查 logs/error 目录下日志文件', "")
}
})
// routes
app.use(httpRoute.routes(), httpRoute.allowedMethods())
app.ws.use(wsRoute.routes(), wsRoute.allowedMethods())
// error-handling
app.on('error', (err, ctx) => {
console.error('server error', err, ctx)
});
module.exports = app
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/1063740214/webfunny_monitor.git
git@gitee.com:1063740214/webfunny_monitor.git
1063740214
webfunny_monitor
webfunny_monitor
master

搜索帮助