-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnote.js
More file actions
58 lines (49 loc) · 1.37 KB
/
note.js
File metadata and controls
58 lines (49 loc) · 1.37 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
const express = require('express');
const mongoose = require('mongoose');
const path = require('path');
const bodyParser = require('body-parser');
const app = express();
const noteSchema = new mongoose.Schema({
title: String,
content: String
});
const Note = mongoose.model('Note', noteSchema);
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static(path.join(__dirname, 'public')));
app.get('/', (req, res) => {
res.render('index');
});
app.get('/showpage', (req, res) => {
mongoose.connect('mongodb://localhost:27017/stickynotesdb', {
useNewUrlParser: true,
}).then(()=>{
Note.find().then((result)=>{
res.render('show', {
notes: result
})
mongoose.disconnect()
}).catch((err)=>{
console.error(err);
})
});
})
app.post('/notes', async (req, res) => {
mongoose.connect('mongodb://localhost:27017/stickynotesdb', {
useNewUrlParser: true,
}).then(()=>{
Note.create({
title: req.body.title,
content: req.body.content
}).then(()=>{
mongoose.disconnect()
res.redirect('/showpage');
})
}).catch((err)=>{
console.error(err);
})
});
app.listen(3000, () => {
console.log(`Server running on port 3000`);
});