-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
65 lines (56 loc) · 1.83 KB
/
app.js
File metadata and controls
65 lines (56 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
require("dotenv").config();
const express = require("express");
const oauthRoutes = require("./routes/oauth-request-route");
const callbackRoutes = require("./routes/get-token-route");
const app = express();
const PORT = process.env.PORT || 8080;
// ミドルウェアの設定
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// CORS設定(必要に応じて)
app.use((req, res, next) => {
res.header("Access-Control-Allow-Origin", "*");
res.header(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept, Authorization"
);
res.header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
if (req.method === "OPTIONS") {
res.sendStatus(200);
} else {
next();
}
});
// ルートの設定 - 各ルートファイルを独立してマウント
app.use("/api/oauth", oauthRoutes);
app.use("/api/oauth", callbackRoutes);
// ヘルスチェックエンドポイント
app.get("/health", (req, res) => {
res.json({ status: "OK", message: "OAuth server is running" });
});
// 404ハンドラー
app.use("*", (req, res) => {
res.status(404).json({ error: "Route not found" });
});
// エラーハンドラー
app.use((error, req, res, next) => {
console.error("Unhandled error:", error);
res.status(500).json({
error: "Internal server error",
message:
process.env.NODE_ENV === "development"
? error.message
: "Something went wrong",
});
});
// サーバーの起動
app.listen(PORT, () => {
console.log(`OAuth server is running on port ${PORT}`);
console.log(
`OAuth authorization endpoint: http://localhost:${PORT}/api/oauth/authorize`
);
console.log(
`OAuth callback endpoint: http://localhost:${PORT}/api/oauth/get-token`
);
});
module.exports = app;