DailyHotApi/index.js

100 lines
2.3 KiB
JavaScript
Raw Normal View History

2023-03-14 08:04:10 +00:00
require("dotenv").config();
const Koa = require("koa");
const bodyParser = require("koa-bodyparser");
const cors = require("koa2-cors");
const serve = require("koa-static");
const views = require("koa-views");
const app = new Koa();
const net = require("net");
const router = require("./routes");
// 配置信息
let domain = process.env.ALLOWED_DOMAIN || "*";
let port = process.env.PORT || 6688;
// 解析请求体
app.use(bodyParser());
// 静态文件目录
app.use(serve(__dirname + "/public"));
app.use(views(__dirname + "/public"));
// 跨域
app.use(
cors({
origin: domain,
}),
2023-03-14 08:04:10 +00:00
);
2024-01-17 07:07:51 +00:00
// CORS
2023-03-14 08:04:10 +00:00
app.use(async (ctx, next) => {
2024-01-17 07:07:51 +00:00
ctx.set("Access-Control-Allow-Origin", domain);
ctx.set("Access-Control-Allow-Methods", "GET, OPTIONS");
ctx.set("Access-Control-Allow-Headers", "Content-Type, Authorization");
ctx.set("Access-Control-Allow-Credentials", "true");
// 处理预检请求
if (ctx.method === "OPTIONS") {
ctx.status = 200;
2023-03-14 08:04:10 +00:00
} else {
2024-01-17 07:07:51 +00:00
if (domain === "*") {
2023-03-20 07:50:16 +00:00
await next();
} else {
2024-01-17 07:07:51 +00:00
if (ctx.headers.origin === domain || ctx.headers.referer === domain) {
await next();
} else {
ctx.status = 403;
ctx.body = {
code: 403,
message: "请通过正确的域名访问",
};
}
2023-03-14 08:04:10 +00:00
}
}
});
// 使用路由中间件
app.use(router.routes());
app.use(router.allowedMethods());
// 启动应用程序并监听端口
const startApp = (port) => {
app.listen(port, () => {
2023-12-06 01:38:29 +00:00
console.info(`成功在 ${port} 端口上运行`);
2023-03-14 08:04:10 +00:00
});
};
// 检测端口是否被占用
const checkPort = (port) => {
return new Promise((resolve, reject) => {
const server = net
.createServer()
.once("error", (err) => {
if (err.code === "EADDRINUSE") {
2023-12-06 01:38:29 +00:00
console.info(`端口 ${port} 已被占用, 正在尝试其他端口...`);
2023-03-14 08:04:10 +00:00
server.close();
resolve(false);
} else {
reject(err);
}
})
.once("listening", () => {
server.close();
resolve(true);
})
.listen(port);
});
};
// 尝试启动应用程序
const tryStartApp = async (port) => {
let isPortAvailable = await checkPort(port);
while (!isPortAvailable) {
port++;
isPortAvailable = await checkPort(port);
}
startApp(port);
};
tryStartApp(port);