-
Notifications
You must be signed in to change notification settings - Fork 10
/
data.js
69 lines (56 loc) · 1.53 KB
/
data.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
const express = require('express');
const app = express();
app.use(express.urlencoded({ extended: true }));
function template(name = '', email = '', ssn = '') {
return `
<form method="post" action="/post">
<label>Nafn: <input required type="text" name="name" value="${name}"></label>
<label>Netfang: <input required type="email" name="email" value="${email}"></label>
<label>
Kennitala:
<input
required
type="text"
pattern="^[0-9]{6}-?[0-9]{4}$"
name="ssn"
value="${ssn}"
>
</label>
<button>Senda</button>
</form>
`;
}
app.get('/', (req, res) => {
res.send(template());
});
app.post('/post', (req, res) => {
const {
name = '',
email = '',
ssn = '',
} = req.body;
const errors = [];
// Þetta er bara validation! Ekki sanitization
if (name === '') {
errors.push('Nafn má ekki vera tómt');
}
if (email === '' || email.indexOf('@') < 0) {
errors.push('Netfang má ekki vera tómt og verður að innihalda @');
}
if (ssn === '' || /^[0-9]{6}-?[0-9]{4}$/.test(ssn)) {
errors.push('Kennitala má ekki vera tóm og verður að vera tíu tölustafir');
}
if (errors.length > 0) {
return res.send(`${template(name, email, ssn)}
<p>Villur:</p>
<ul>
<li>${errors.join('</li><li>')}</li>
</ul>`);
}
return res.send('<p>Skráning móttekin</p>');
});
const hostname = '127.0.0.1';
const port = 3000;
app.listen(port, hostname, () => {
console.info(`Server running at http://${hostname}:${port}/`);
});