forked from tntp/Tntp.CodingExercise
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
67 lines (53 loc) · 1.99 KB
/
server.js
File metadata and controls
67 lines (53 loc) · 1.99 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
67
// We first require our express package
var express = require('express');
var bodyParser = require('body-parser');
var commentData = require('./data.js');
// This package exports the function to create an express instance:
var app = express();
// We can setup Jade now!
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'));
app.get("/getposts", function(request, response) {
//console.log('Getting all posts.');
commentData.getAllComments().then(
function(params) {
response.json(params);
});
});
app.get("/", function(request, response) {
//console.log('Naviagting to home page');
commentData.getAllComments().then(
function(params) {
response.render("pages/home", { error: "", comments: params });
});
});
app.post("/createpost", function(request, response) {
var comment = request.body.comment;
var name = request.body.name;
commentData.createComment(comment, name)
.then(
function(ID) {
response.redirect("/");
},
function(errorMessage) {
var jsonData = { error: errorMessage };
response.render("pages/home", jsonData);
});
});
app.get("/all1", function(request, response) {
commentData.getAllComments().then(
function(params) {
console.log(params);
response.json(params);
});
});
// 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');
});