-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
66 lines (60 loc) · 1.86 KB
/
index.js
File metadata and controls
66 lines (60 loc) · 1.86 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
66
require("dotenv").config();
const express = require("express");
const app = express();
const pool = require("./db");
app.use(express.json()) // => req.body
// ROUTES //
// Checking if the server is listening to our queries
app.listen(5000, () => {
console.log("Server is Listening on Port 5000");
});
// Users Register Data
app.post('/userRegister', async (req, res) => {
try {
const {
firstname,
lastname,
username,
password
} = req.body;
const RegisterUsers = await pool.query("INSERT INTO users (firstname, lastname, username, password) VALUES ($1, $2, $3, $4) RETURNING *",
[firstname, lastname, username, password]
);
res.json(RegisterUsers.rows);
} catch (error) {
console.error(error.message);
}
});
// Teachers Register Data
app.post('/teacherRegister', async (req, res) => {
try {
const {
tfirstname,
tlastname,
tusername,
tpassword,
} = req.body;
const RegisterTeacher = await pool.query("INSERT INTO teachers (tfirstname, tlastname, tusername, tpassword) VALUES ($1, $2, $3, $4) RETURNING *",
[tfirstname, tlastname, tusername, tpassword]
);
res.json(RegisterTeacher.rows);
} catch (error) {
console.error(error.message);
}
});
// Answers Submission
app.post('/userAnswers', async (req, res) => {
try {
const {
users_id,
answer_selected,
correct_answer
} = req.body;
const SelectedAnswer = await pool.query("INSERT INTO answers (users_id, answer_selected, correct_answer) VALUES ($1, $2, $3) RETURNING *",
[users_id, answer_selected, correct_answer]
);
res.json(SelectedAnswer.rows);
} catch (err) {
console.error(err.message);
}
});