-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
68 lines (55 loc) · 1.63 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
const express = require('express');
const app = express()
const bodyparser = require('body-parser')
const methodoverride = require('method-override')
const mongoose = require('mongoose')
mongoose.set('useUnifiedTopology', true);
mongoose.set('useNewUrlParser', true);
app.use(express.static(__dirname + "/public"));
mongoose.connect('mongodb://heroku_02cn163v:[email protected]:29078/heroku_02cn163v');
app.use(bodyparser.urlencoded({ extended: true }));
app.use(methodoverride('_method'))
app.set('view engine', 'ejs');
const infoSchema = new mongoose.Schema({
'name': String,
'created': { type: Date, default: Date.now },
'title': String,
'questions': String,
'answer': [String]
})
const info = mongoose.model('info', infoSchema)
app.get('/', (req, res) => {
res.redirect('/qBlog')
})
app.get('/qBlog', (req, res) => {
info.find({}, (err, info) => {
if (err) {
console.log('something went wrong')
} else {
res.render('qBlog', { info: info })
}
})
})
app.get('/qBlog/ask', (req, res) => {
res.render('Ask')
})
app.post('/qBlog', (req, res) => {
info.create({
name: req.body.name,
questions: req.body.question,
title:req.body.course
})
res.redirect('qBlog')
})
app.put('/qBlog/:id', (req, res) => {
info.findByIdAndUpdate(req.params.id, { $push: { answer: req.body.answer } }, (err, updatedanswer) => {
if (err) {
console.log(error)
} else {
res.redirect('/')
}
})
})
app.listen(process.env.PORT || 3000, () => {
console.log('server started')
})