-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIndex.js
106 lines (92 loc) · 2.72 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
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const connection = require('./database/database');
const QuestionsModel = require('./database/Questions');
const AnswerModel = require('./database/Answer');
// Database
connection
.authenticate()
.then(() => {
console.log('Connected with the database!')
})
.catch((err) => {
console.log('err')
})
// "Informing" the express to use the EJS as view engine
app.set('view engine', 'ejs');
// Define I want to use static archives
app.use(express.static('public'));
// bodyParser => translate the data that is coming from the form
app.use(bodyParser.urlencoded({extended: false}))
app.use(bodyParser.json());
// app.use(bodyParser());
// routes
app.get("/", (req, res) => {
// findAll() => will search all questions on the table and return them to us.
QuestionsModel.findAll({
raw: true, order: [
['id', 'DESC'] // or ASC
]
}).then(questions => {
res.render("index", {
questions: questions
})
})
})
app.get("/ask", (req, res) => {
res.render("ask")
})
app.post("/saveQuestions", (req, res) => {
console.log(req.body)
// req data from the form by the => name
let title = req.body.title;
let description = req.body.description;
// To save the questions in a table. First we have to assign it to a var => questionsModel and then call the method creat()
QuestionsModel.create({
title: title,
description: description
}).then(() => {
res.redirect("/");
})
})
app.get("/question/:id", (req, res) => {
let id = req.params.id;
QuestionsModel.findOne({
where: {id: id}
}).then(question => {
if (question != undefined) {
AnswerModel.findAll({
where: {questionId: question.id},
order: [['id', 'DESC']]
}).then((answers) => {
res.render("question", {
question: question,
answers: answers
})
})
} else {
res.redirect("/");
}
})
})
app.post("/answer", (req, res) => {
/*this route will receive the data from the answer form*/
let body = req.body.body;
let questionId = req.body.question;
console.log(body);
console.log(questionId)
AnswerModel.create({
body: body,
questionId: questionId
}).then(() => {
res.redirect("/question/" + questionId);
}).catch((e) => {
console.log(e);
res.redirect("/question/" + questionId);
})
})
const port = process.env.PORT || 8080;
app.listen(port, () => {
console.log("App is running at the port", port);
})