forked from mluukkai/example_app
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
160 lines (135 loc) · 3.33 KB
/
index.js
File metadata and controls
160 lines (135 loc) · 3.33 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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
const express = require('express')
const path = require('path')
const bodyParser = require('body-parser')
const PORT = process.env.PORT || 5000
const MAX_NOTES = 100;
const PATH_PREFIX = '/exampleapp';
const app = express()
app.use(bodyParser())
const notes = [
{
content: 'HTML is easy',
date: new Date('2019-05-23T17:30:31.098Z'),
},
{
content: 'Browser can execute only Javascript',
date: new Date('2019-05-23T18:39:34.091Z'),
},
{
content: 'Most important methods of HTTP-protocol are GET and POST',
date: new Date('2019-05-23T19:20:14.298Z'),
},
]
const isValidNote = note => {
return typeof note === 'object' && typeof note.content === 'string' && !isNaN(new Date(note.date).getTime())
}
const createNote = note => {
notes.push(note);
if (notes.length > MAX_NOTES) {
notes.shift()
}
}
const formatNote = note => {
return {
content: note.content.substring(0, 200),
date: new Date(note.date),
}
}
const notes_page = `
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="${PATH_PREFIX}/main.css" />
<script type="text/javascript" src="${PATH_PREFIX}/main.js"></script>
</head>
<body>
<div class='container'>
<h1>Notes</h1>
<div id='notes'>
</div>
<form action='${PATH_PREFIX}/new_note' method='POST'>
<input type="text" name="note"><br>
<input type="submit" value="Save">
</form>
</div>
</body>
</html>
`
const notes_spa = `
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="${PATH_PREFIX}/main.css" />
<script type="text/javascript" src="${PATH_PREFIX}/spa.js"></script>
</head>
<body>
<div class='container'>
<h1>Notes -- single page app</h1>
<div id='notes'>
</div>
<form id='notes_form'>
<input type="text" name="note"><br>
<input type="submit" value="Save">
</form>
</div>
</body>
</html>
`
const getFrontPageHtml = (noteCount) => {
return(`
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div class='container'>
<h1>Full stack example app</h1>
<p>number of notes created ${noteCount}</p>
<a href='${PATH_PREFIX}/notes'>notes</a>
<img src='kuva.png' width='200' />
</div>
</body>
</html>
`)
}
const router = express.Router();
router.use(express.static(path.join(__dirname, 'public')))
router.get('/', (req, res) => {
const page = getFrontPageHtml(notes.length)
res.send(page)
})
router.get('/reset', (req, res) => {
notes.splice(0, notes.length)
res.status(201).send({ message: 'notes reset' })
})
router.get('/notes', (req, res) => {
res.send(notes_page)
})
router.get('/spa', (req, res) => {
res.send(notes_spa)
})
router.get('/data.json', (req, res) => {
res.json(notes)
})
router.post('/new_note_spa', (req, res) => {
if (!isValidNote(req.body)) {
return res.send('invalid note').status(400)
}
createNote(formatNote(req.body))
res.status(201).send({ message: 'note created' })
})
router.post('/new_note', (req, res) => {
if (typeof req.body.note === 'string') {
createNote(formatNote({
content: req.body.note,
date: new Date()
}))
}
res.redirect(`${PATH_PREFIX}/notes`)
})
if (process.env.NODE_ENV === 'development') {
app.use(PATH_PREFIX, router)
} else {
app.use('/', router)
}
app.listen(PORT, () => console.log(`Listening on ${PORT}`))