-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
65 lines (52 loc) · 1.61 KB
/
server.js
File metadata and controls
65 lines (52 loc) · 1.61 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
import express from "express";
import pool from "./db.js";
const app = express();
app.use(express.json())
const port = process.env.PORT || 3000;
app.get("/create", async (req, res) =>{
try {
await pool.query("CREATE TABLE schools (id SERIAL PRIMARY KEY, name VARCHAR(50), email VARCHAR(50))");
res.status(201).send("Table created successfully");
} catch (error) {
console.log(error.message);
}
})
app.post("/", async (req, res) => {
const { name, email } = req.body;
try {
await pool.query ("INSERT INTO schools (name, email) VALUES ($1, $2)", [name, email]);
res.send("Entry added successfully")
} catch (err) {
console.error(err.message);
}
})
app.get("/", async (req, res) => {
try {
const data = await pool.query ("SELECT * FROM schools");
res.send(data.rows);
} catch (err) {
console.error(err.message);
}
});
app.put("/:id", async (req, res) => {
try {
const { id } = req.params;
const { name, email } = req.body;
await pool.query("UPDATE schools SET name = $1, email = $2 WHERE id = $3", [name, email, id]);
res.send("Entry updated successfully");
} catch (error) {
console.log(error.message);
}
})
app.delete("/:id", async (req, res) =>{
try {
const { id } = req.params;
await pool.query("DELETE FROM schools WHERE id = $1", [id]);
res.send("Entry deleted successfully");
} catch (error) {
console.log(error.message);
}
})
app.listen(port, () => {
console.log("Server is running on port 3000");
});