-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
90 lines (74 loc) · 2.33 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
const express = require('express')
const app = new express()
const path = require('path')
const ejs = require('ejs')
const moment = require('moment')
const fileUpload = require('express-fileupload')
app.set('view engine', 'ejs')
const bodyParser = require('body-parser')
app.use(bodyParser.urlencoded({ extended: true }))
app.use(bodyParser.json({ type: 'application/json' }))
app.use(bodyParser.raw())
app.use(fileUpload())
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/my_database', { useNewUrlParser: true })
const BlogPost = require('./app/models/BlogPost.js')
app.use(express.static(__dirname + '/public'))
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({extended:true}))
app.set('view engine', 'ejs')
app.listen(8080, () => {
console.log("App listening on port 8080")
})
app.get('/', (request, response) => {
//response.sendFile(path.resolve(__dirname, 'index.html'))
BlogPost.find({}, function (error, posts) {
console.log(posts);
response.render('index', {
blogposts: posts,
moment: moment
});
})
})
app.get('/about', (request, response) => {
//response.sendFile(path.resolve(__dirname, 'about.html'))
response.render('about')
})
app.get('/contact', (request, response) => {
//response.sendFile(path.resolve(__dirname, 'contact.html'))
response.render('contact')
})
app.get('/post', (request, response) => {
//response.sendFile(path.resolve(__dirname, 'post.html'))
response.render('post')
})
app.get('/post/:id', (req, res) => {
BlogPost.findById(req.params.id, function(error, detailPost){
res.render('post', {
detailPost
})
})
})
app.get('/posts/new', (req, res) => {
res.render('create')
})
app.post('/posts/store', (req, res) => {
// console.log(req.body)
// res.redirect('/')
let image = req.files.image
image.mv(path.resolve(__dirname, 'public/upload', image.name), function(err){
BlogPost.create({...req.body,
image: '/upload/' + image.name},
function(err){
res.redirect('/')
})
})
})
// app.get('/', (request, response) => {
// BlogPost.find({}, function (error, posts) {
// console.log(posts);
// response.render('index', {
// blogposts: posts
// });
// })
// })