forked from ritza-co/fullcalendar-migration-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
79 lines (67 loc) · 1.87 KB
/
server.js
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
import express from "express";
import bodyParser from "body-parser";
import mysql from "mysql2";
import cors from "cors";
import dotenv from "dotenv";
import path from "path";
import { fileURLToPath } from "url";
dotenv.config();
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const app = express();
app.use(cors());
app.use(bodyParser.json());
app.use(express.static(path.join(__dirname, "public")));
const db = mysql.createConnection({
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_DATABASE,
});
db.connect((err) => {
if (err) throw err;
console.log("Connected to MySQL database");
});
app.get("/events", (req, res) => {
db.query("SELECT * FROM events", (err, results) => {
if (err) throw err;
res.send(results);
});
});
app.post("/events", (req, res) => {
console.log("Received POST request on /events");
console.log("Received data:", req.body);
const { title, start, end } = req.body;
db.query(
"INSERT INTO events (title, start, end) VALUES (?, ?, ?)",
[title, start, end],
(err, result) => {
if (err) throw err;
console.log("Event added with ID:", result.insertId);
res.send({ id: result.insertId });
}
);
});
app.put("/events/:id", (req, res) => {
const { id } = req.params;
const { start, end } = req.body;
db.query(
"UPDATE events SET start = ?, end = ? WHERE id = ?",
[start, end, id],
(err, result) => {
if (err) throw err;
res.sendStatus(200);
}
);
});
app.delete("/events/:id", (req, res) => {
const { id } = req.params;
db.query("DELETE FROM events WHERE id = ?", [id], (err, result) => {
if (err) throw err;
res.sendStatus(200);
});
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});