-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
70 lines (60 loc) · 1.62 KB
/
app.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
const express = require("express");
// IMPORTING THE CLIENT FOR USING THE FUNCTIONS WE DECLARED IN PROTO FILE
const client = require("./client");
const path = require("path");
const { employees } = require("./dummyEmp");
const app = express();
app.set("views", path.join(__dirname, "views"));
app.set("view engine", "pug");
app.use(express.json());
app.use(
express.urlencoded({
extended: false,
})
);
app.get("/", (req, res) => {
// Getting getAll function from the client
client.getAll({}, (err, response) => {
if (!err) {
res.render("home", { details: response.message });
} else {
console.log(err.details);
}
});
});
app.get("/:id", (req, res) => {
/*
the getDetails that we sent from the server to let the client use it is used here
this getDetails takes a id as parameter as we defined in the proto file then gives out a response needed.
*/
client.getDetails({ id: req.params.id }, (err, response) => {
if (!err) {
res.render("details", { details: response.message });
} else {
console.log(err.details);
}
});
});
app.post("/insert", (req, res) => {
// Getting Insert function from the client
client.insert(
{
email: req.body.email,
firstName: req.body.firstName,
lastName: req.body.lastName,
},
(err, response) => {
if (!err) {
console.log("Success");
res.redirect("/");
console.log(response);
} else {
console.log(err.details);
}
}
);
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Process ${process.pid} Running on port ` + PORT);
});