-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
143 lines (121 loc) · 5.55 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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// We first require our express package
var express = require('express');
var bodyParser = require('body-parser');
var questionAndAnswers = require('./data.js')
// This package exports the function to create an express instance:
var app = express();
// Here we change our view engine from Jade (default) to EJS
app.set('view engine', 'ejs');
// This is called 'adding middleware', or things that will help parse your request
app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
// This middleware will activate for every request we make to
// any path starting with /assets;
// it will check the 'static' folder for matching files
app.use('/assets', express.static('static'));
// check for hidden input with the tag _method
// browser sometimes cannot submit a PUT or DELETE request,
// so we can use middleware to change it before it hits our routes!
// I have programatically injected some code that will add a hidden input called _method
app.use(function (req, res, next) {
if (req.body && req.body._method) {
console.log(req.body);
req.method = req.body._method;
delete req.body._method;
}
// let the next middleware run:
next();
});
// Setup your routes here!
app.get("/", function (request, response) {
// This will redirect to a different route; the user will end up at /questions automatically
response.redirect("/questions");
});
app.get("/all_the_forms", function (request, response) {
response.render('pages/forms', { pageTitle: "Testing all the inputs" });
});
app.post("/all_the_forms", function (request, response) {
var tableData = [];
// our request.body will get populated as an object
for (var key in request.body) {
var data = request.body[key];
var entry = {
inputName: key,
inputValue: JSON.stringify(data),
inputType: typeof data
};
tableData.push(entry);
}
response.render('pages/forms', { pageTitle: "Testing all the inputs", tableData: tableData });
});
app.get("/questions/:id", function (request, response) {
try {
var question = questionAndAnswers.getQuestion(request.params.id);
// we caught an exception! Let's show an error page!
response.render('pages/question', { question: question, pageTitle: question.title });
} catch (message) {
// we caught an exception! Let's show an error page!
response.status(500).render('pages/error', { errorType: "Issue loading question!", errorMessage: message });
}
});
// Make a new question
app.post("/questions", function (request, response) {
console.log("POSTING REQUEST WITH BODY OF:");
console.log(request.body);
try {
var question = questionAndAnswers.addQuestion(request.body.title, request.body.text);
response.render('pages/question', { question: question, pageTitle: question.title });
} catch (message) {
// we caught an exception! Let's show an error page!
response.status(500).render('pages/error', { errorType: "Issue creating question!", errorMessage: message });
}
});
// let's add an answer!
app.post("/questions/:id/answer", function (request, response) {
try {
var question = questionAndAnswers.answerQuestion(request.params.id, request.body.answer);
response.render('pages/question', { question: question, pageTitle: question.title });
} catch (message) {
// we caught an exception! Let's show an error page!
response.status(500).render('pages/error', { errorType: "Cannot answer question!", errorMessage: message });
}
});
// Update a question
app.put("/questions/:id", function (request, response) {
console.log("PUTTING REQUEST WITH BODY OF:");
console.log(request.body);
try {
var question = questionAndAnswers.updateQuestion(request.params.id, request.body.title, request.body.text);
// we caught an exception! Let's show an error page!
response.render('pages/question', { question: question, pageTitle: question.title });
} catch (message) {
// we caught an exception! Let's show an error page!
response.status(500).render('pages/error', { errorType: "Issue updating question!", errorMessage: message });
}
})
app.get("/questions", function (request, response) {
// We have to pass a second parameter to specify the root directory
// __dirname is a global variable representing the file directory you are currently in
var questionType = request.query.type,
displayType = "",
questionsToShow = [];
// First, we retrieve our data
if (questionType === "answered") {
displayType = "Answered Questions";
questionsToShow = questionAndAnswers.getAnsweredQuestions();
} else if (questionType === "all") {
displayType = "All Questions";
questionsToShow = questionAndAnswers.getAllQuestions();
} else {
displayType = "Unanswered Questions";
questionsToShow = questionAndAnswers.getUnansweredQuestions();
}
// render will search your 'views' directory and follow the path you give it to get a template
// it will compile the template with the model you provide in the second parameter and
// send it to the user
response.render('pages/index', { questions: questionsToShow, type: displayType, pageTitle: "Home" });
});
// We can now navigate to localhost:3000
app.listen(3000, function () {
console.log('Your server is now listening on port 3000! Navigate to http://localhost:3000 to access it');
});