-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
69 lines (60 loc) · 1.67 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
const express = require("express");
const app = express();
const Sequelize = require("sequelize");
const HTTP_PORT = process.env.PORT || 8080;
app.set("view engine", "ejs");
const sequelize = new Sequelize("database", "user", "password", {
host: "host",
dialect: "postgres",
port: 5432,
dialectOptions: {
ssl: { rejectUnauthorized: false }
}
});
const Name = sequelize.define("Name", {
fName: Sequelize.STRING, // first Name
lName: Sequelize.STRING, // Last Name
});
app.use(express.static("public"));
app.use(express.urlencoded({ extended: true }));
app.get("/", (req, res) => {
Name.findAll({
order: ["id"]
}).then((data) => {
res.render("home", { data });
});
});
app.post("/updateName", (req, res) => {
if (req.body.lName.length == 0 && req.body.fName.length == 0) {
Name.destroy({
where: { id: req.body.id }
}).then(() => {
console.log("successfully removed name: " + req.body.id);
res.redirect("/");
});
} else {
Name.update({
lName: req.body.lName,
fName: req.body.fName
}, {
where: { id: req.body.id }
}).then(() => {
console.log("successfully updated name: " + req.body.id);
res.redirect("/");
});
}
});
app.post("/addName", (req, res) => {
Name.create({
lName: req.body.lName,
fName: req.body.fName
}).then(() => {
console.log("successfully created a new name");
res.redirect("/");
});
});
sequelize.sync().then(() => {
app.listen(HTTP_PORT, ()=>{
console.log(`server listening on: ${HTTP_PORT}`);
});
});