-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
96 lines (77 loc) · 2.82 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
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
var express = require('express') // Express Framework
var engines = require('consolidate') // Helps us set the templating engine 'nunjucks'
var mongoClient = require('mongodb').MongoClient // The Mongodb Driver
var assert = require('assert') // Assertion library to cath errors
var bodyParser = require('body-parser') // Middleware to parse url parameters
var PostDAO = require('./post').PostDAO
var app = express() // Express app
app.engine('html', engines.nunjucks); // Set nunjucks as the templating engine to work on documents ending with the extension html
app.set('view engine', 'html') // Only use nunjucks on file ending with html
app.set('views', __dirname + '/views') // Where to find the view files
app.use('/static', express.static(__dirname + '/static')); // Use static files
app.use(bodyParser.urlencoded({extended: true})) // Url parser configuration
var databaseName = 'hacker-news' // Set to the mondodb database to be use
mongoClient.connect('mongodb://localhost:27017/'+databaseName, function(err, db){
assert.equal(null, err);
console.log('Successfully connected to mongodb database')
// MODELS ***********************************
var post = new PostDAO(db)
post.seedCollection(function(count){
if (count === 0) {
post.insertMany()
}
})
// ROUTES ***********************************
app.get('/', function(req, res){
post.getAllPosts(function(posts){
res.render('index', {posts: posts})
})
})
app.post('/posts', function(req, res){
var title = req.body.title
var author = req.body.author
var url = req.body.url
post.addPost(title, author, url, function(postDoc){
res.redirect('/')
})
})
app.get('/posts/:id/edit', function(req, res){
var id = req.params.id
post.getPost(id, function(postDoc){
res.render('edit', {post: postDoc})
})
})
app.post('/posts/:id/update', function(req, res){
var id = req.params.id
var title = req.body.title
var author = req.body.author
var url = req.body.url
post.updatePost(id, title, author, url, function(postDoc){
res.redirect('/')
})
})
app.get('/posts/:id/votes', function(req, res){
var id = req.params.id
post.addPostVote(id, function(postDoc){
res.redirect('/')
})
})
app.get('/posts/:id/delete', function(req, res){
var id = req.params.id
post.deletePost(id, function(postDoc){
console.log(postDoc)
res.redirect('/')
})
})
// Error Handling and Start running server
app.use(errorHandler)
function errorHandler(err, req, res, next) {
console.error(err.message);
console.error(err.stack);
res.status(500).render('errorTemplate', { error: err });
}
var server = app.listen(3000, function(){
var port = server.address().port
console.log('Express server listening on port %s.', port);
})
})