-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathserver5.js
37 lines (29 loc) · 1.09 KB
/
server5.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
// Here we store the web page un public directory and serve it using express.static.
const express = require('express');
const bodyParser = require('body-parser');
const escape = require('escape-html');
const app = express();
const port = 3000;
// Array to store names and emails
let users = [];
app.use(bodyParser.json());
app.use(express.static('public'));
// Serve the web page with the form
// note __dirname is the directory in which node Web Server is running
app.get('/', function(req, res) {
res.sendFile(__dirname + '/public/index1.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 running at http://localhost:${port}/`);
});
// This code is similar to the previous example, but it uses the fetch API to submit the form data asynchronously.