forked from mluukkai/example_app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
124 lines (107 loc) · 2.45 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
const express = require('express')
const path = require('path')
const bodyParser = require('body-parser')
const PORT = process.env.PORT || 5000
const app = express()
app.use(express.static(path.join(__dirname, 'public')))
app.use(bodyParser())
const notes = [
{
content: 'HTML is easy',
date: '2019-05-23T17:30:31.098Z'
},
{
content: 'Browser can execute only Javascript',
date: '2019-05-23T18:39:34.091Z'
},
{
content: 'Most important methods of HTTP-protocol are GET and POST',
date: '2019-05-23T19:20:14.298Z'
},
]
const notes_page = `
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="/main.css" />
<script type="text/javascript" src="main.js"></script>
</head>
<body>
<div class='container'>
<h1>Notes</h1>
<div id='notes'>
</div>
<form action='/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="/main.css" />
<script type="text/javascript" src="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 getFronPageHtml = (noteCount) => {
return(`
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div class='container'>
<h1>Full stack exampe app</h1>
<p>number of notes created ${noteCount}</p>
<a href='/notes'>notes</a>
<img src='kuva.png' width='200' />
</div>
</body>
</html>
`)
}
app.get('/', (req, res) => {
const page = getFronPageHtml(notes.length)
res.send(page)
})
app.get('/reset', (req, res) => {
notes.splice(0, notes.length)
res.status(201).send({ message: 'notes reset' })
})
app.get('/notes', (req, res) => {
res.send(notes_page)
})
app.get('/spa', (req, res) => {
res.send(notes_spa)
})
app.get('/data.json', (req, res) => {
res.json(notes)
})
app.post('/new_note_spa', (req, res) => {
notes.push(req.body)
res.status(201).send({ message: 'note created'})
})
app.post('/new_note', (req, res) => {
notes.push( {
content: req.body.note,
date: new Date()
})
res.redirect('/notes')
})
app.listen(PORT, () => console.log(`Listening on ${PORT}`))