-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
108 lines (90 loc) · 2.85 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
const express = require('express');
const pug = require('pug');
const bodyParser = require('body-parser');
const marked = require('marked');
const { v4 } = require('uuid');
const compression = require('compression');
const PORT = process.env.PORT || 3000;
const app = express();
let notes = require('./data/notes');
app.set('view engine', 'pug');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.use(express.static('assets'));
app.use(compression());
app.get('/', (req, res) => {
res.render('index', { notes });
});
app.get('/note/:id', (req, res) => {
const { id } = req.params;
const note = notes.find(n => n.id == id);
const template = pug.compileFile('views/_note.pug');
const markdown = marked(note.content);
const markup = template({ note, markdown});
res.send(markup);
});
app.get('/new', (req, res) => {
const template = pug.compileFile('views/_new-note.pug');
const markup = template({ });
res.send(markup);
});
app.post('/preview', (req, res) => {
const { draft } = req.body;
const markup = marked(draft);
res.send(markup);
});
app.post('/note', (req,res) => {
console.log(req.body);
const { title, draft } = req.body;
const template = pug.compileFile('views/_note.pug');
const markdown = marked(draft);
const note = {
id: v4(),
title,
content: draft,
createdAt: new Date().toLocaleString()
};
notes.unshift(note);
const tmpltNotesList = pug.compileFile('views/_notes-list.pug');
let markup = tmpltNotesList({ notes });
markup += template({ note, markdown});
res.send(markup);
});
app.put('/note/:id', (req,res) => {
console.log(req.body);
const { id } = req.params;
const note = notes.find(n => n.id == id);
const { title, draft } = req.body;
const template = pug.compileFile('views/_note.pug');
const markdown = marked(draft);
note.title = title;
note.content = draft;
const markup = template({ note, markdown});
res.send(markup);
});
app.get('/edit/:id', (req, res) => {
const { id } = req.params;
const note = notes.find(n => n.id == id);
const template = pug.compileFile('views/_edit-note.pug');
const markdown = marked(note.content);
const markup = template({ note, markdown });
res.send(markup);
});
app.delete('/note/:id', (req, res) => {
const { id } = req.params;
notes = notes.filter(n => n.id != id);
const template = pug.compileFile('views/_notes-list.pug');
let markup = template({ notes });
const emptyTemplate = pug.compileFile('views/_empty.pug');
markup += emptyTemplate();
res.send(markup);
});
app.post('/search', (req, res) => {
const { query } = req.body;
const results = notes.filter(n => n.title.toLowerCase().includes(query));
const template = pug.compileFile('views/_notes-list.pug');
let markup = template({ notes: results });
res.send(markup);
});
app.listen(PORT);
console.log('Listening on port: ', PORT);