-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
120 lines (104 loc) · 3.11 KB
/
server.js
File metadata and controls
120 lines (104 loc) · 3.11 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
require("dotenv").config();
const express = require("express");
const { Client } = require("@notionhq/client");
const path = require("path");
const app = express();
const port = process.env.PORT || 3000;
// Initialize Notion client
const notion = new Client({ auth: process.env.NOTION_KEY });
const databaseId = process.env.NOTION_DATABASE_ID;
// Middleware
app.use(express.static("public")); // Serve static files from /public
app.use(express.json()); // Parse incoming JSON payloads
// Serve homepage
app.get("/", (req, res) => {
res.sendFile(path.join(__dirname, "views", "index.html"));
});
// Test route to confirm the server is running
app.get("/test", (req, res) => {
res.send("✅ Your Node app is running!");
});
// Debug route to confirm Notion DB access
app.get("/debug-notion-db/:area", async (req, res) => {
const area = req.params.area;
const dbMap = {
world: process.env.NOTION_DB_WORLD,
mods: process.env.NOTION_DB_MODS,
spawnpoint: process.env.NOTION_DB_SPAWNPOINT,
server: process.env.NOTION_DB_SERVER,
ideas: process.env.NOTION_DB_IDEAS,
};
const dbID = dbMap[area];
if (!dbID) {
return res.status(400).json({ message: `No DB ID found for area: ${area}` });
}
try {
const dbInfo = await notion.databases.retrieve({ database_id: dbID });
res.json({
message: `Successfully retrieved DB info for ${area}`,
dbID,
properties: dbInfo.properties,
});
} catch (error) {
console.error("❌ Error retrieving DB info:", JSON.stringify(error, null, 2));
res.status(500).json({ error: error.body || error.message });
}
});
// POST: Create a new page in the selected Notion database
app.post("/submit-area", async (req, res) => {
const { area, pageName, header } = req.body;
const dbMap = {
world: process.env.NOTION_DB_WORLD,
mods: process.env.NOTION_DB_MODS,
spawnpoint: process.env.NOTION_DB_SPAWNPOINT,
server: process.env.NOTION_DB_SERVER,
ideas: process.env.NOTION_DB_IDEAS,
};
const dbID = dbMap[area];
if (!dbID) {
return res.status(400).json({ message: "Invalid area selected." });
}
try {
const newPage = await notion.pages.create({
parent: { database_id: dbID },
properties: {
Name: {
title: [
{
text: {
content: pageName || `New ${area} entry`,
},
},
],
},
},
children: header
? [
{
object: "block",
heading_2: {
rich_text: [
{
text: {
content: header,
},
},
],
},
},
]
: [],
});
res.json({ message: "Page created successfully", data: newPage });
} catch (error) {
console.error("❌ Error creating page:", JSON.stringify(error, null, 2));
res.status(500).json({
message: "Error creating page",
error: error.body || error.message,
});
}
});
// Start the server
app.listen(port, () => {
console.log(`✅ Server running on port ${port}`);
});