1 Star 0 Fork 62

CharlesSong/webfunny_monitor

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
app.js 1.76 KB
一键复制 编辑 原始数据 按行查看 历史
一步一个脚印一个坑 提交于 2021-12-02 00:29 . 跨域问题优化
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 auth = require('./middlreware/auth')
const sqlCheck = require('./middlreware/sqlCheck')
const app = WebSocket(new Koa())
app.use(async (ctx, next) => {
ctx.set("Access-Control-Allow-Origin", ctx.header.origin || "*")
ctx.set("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS")
ctx.set("Access-Control-Allow-Headers", "access-token,webfunny-secret-code")
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(auth())
// middlewares
app.use(bodyParser({
enableTypes: ['json', 'form', 'text'],
formLimit: "50mb",
jsonLimit: "50mb",
textLimit: "50mb"
}))
// 防sql注入
app.use(sqlCheck())
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 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/charlessong/webfunny_monitor.git
git@gitee.com:charlessong/webfunny_monitor.git
charlessong
webfunny_monitor
webfunny_monitor
master

搜索帮助