-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
131 lines (108 loc) · 3.18 KB
/
index.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
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
121
122
123
124
125
126
127
128
129
130
131
// const express = require("express");
// const { Sequelize, DataTypes } = require("sequelize");
// require("dotenv").config();
// const path = require("path");
// const app = express();
// const port = process.env.PORT || 3000;
// app.use(express.json());
// app.use(express.static("public"));
// // Database connection
// const sequelize = new Sequelize(process.env.DATABASE_URL, {
// dialect: "postgres",
// logging: false,
// });
// // Define User model
// const User = sequelize.define("User", {
// name: { type: DataTypes.STRING, allowNull: false },
// email: { type: DataTypes.STRING, unique: true, allowNull: false },
// });
// // Sync database
// sequelize.sync();
// // Routes
// app.get("/users", async (req, res) => {
// const users = await User.findAll();
// res.json(users);
// });
// app.post("/users", async (req, res) => {
// try {
// const user = await User.create(req.body);
// res.status(201).json(user);
// } catch (error) {
// res.status(400).json({ error: error.message });
// }
// });
// app.get("/", (req, res) => {
// res.sendFile(path.join(__dirname, "public", "index.html"));
// });
// app.listen(port, () => {
// console.log(`Server running on port ${port}`);
// });
const express = require("express");
const { Sequelize, DataTypes } = require("sequelize");
require("dotenv").config();
const path = require("path");
const app = express();
const port = process.env.PORT || 3000;
app.use(express.json());
app.use(express.static("public"));
// Database connection
const sequelize = new Sequelize(process.env.DATABASE_URL, {
dialect: "postgres",
logging: false,
});
// Define User model
const User = sequelize.define("User", {
name: { type: DataTypes.STRING, allowNull: false },
email: { type: DataTypes.STRING, unique: true, allowNull: false },
});
// Sync database with retry logic
const connectWithRetry = async () => {
try {
await sequelize.authenticate();
console.log("Database connection has been established successfully.");
await sequelize.sync();
console.log("Database synced.");
} catch (error) {
console.error("Unable to connect to the database:", error);
console.log("Retrying in 5 seconds...");
setTimeout(connectWithRetry, 5000); // Retry after 5 seconds
}
};
connectWithRetry();
// Routes
// Get all users
app.get("/users", async (req, res) => {
const users = await User.findAll();
res.json(users);
});
// Add a new user
app.post("/users", async (req, res) => {
try {
const user = await User.create(req.body);
res.status(201).json(user);
} catch (error) {
res.status(400).json({ error: error.message });
}
});
// Delete a user
app.delete("/users/:id", async (req, res) => {
try {
const user = await User.findByPk(req.params.id);
if (user) {
await user.destroy();
res.status(204).send(); // No content
} else {
res.status(404).json({ error: "User not found" });
}
} catch (error) {
res.status(500).json({ error: error.message });
}
});
// Serve the index.html file
app.get("/", (req, res) => {
res.sendFile(path.join(__dirname, "public", "index.html"));
});
// Start the server
app.listen(port, () => {
console.log(`Server running on port ${port}`);
});