-
Notifications
You must be signed in to change notification settings - Fork 47
/
server6.js
34 lines (26 loc) · 844 Bytes
/
server6.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
const express = require('express');
const bodyParser = require('body-parser');
const escape = require('escape-html');
const app = express();
const port = 3001;
// Array to store names and emails
let users = [];
app.use(bodyParser.json());
app.use(express.static('public'));
// Serve the web page with the form
app.get('/', function(req, res) {
res.sendFile(__dirname + '/public/index2.html');
});
// Handle the form submission via fetch
app.post('/input', function(req, res){
const name = escape(req.body.name);
const email = escape(req.body.email);
// Add the new user to the array
users.push({ name: name, email: email });
// Send the updated list of users back as JSON
res.json(users);
});
// Start the server
app.listen(port, () => {
console.log(`Server 6 running at http://localhost:${port}/`);
});