-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathserver.js
More file actions
35 lines (30 loc) · 949 Bytes
/
server.js
File metadata and controls
35 lines (30 loc) · 949 Bytes
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
let express = require('express');
let bodyParser = require('body-parser');
let path = require('path');
//const ejs = require('ejs'); // Import the ejs module
let app = express();
app.use(bodyParser.json()); // support json encoded bodies
app.use(bodyParser.urlencoded({ extended: true }));
//let ejs = require('ejs');
const port = 3001;
app.use(express.static('./public'));
// serve from public
let times = []; // Array to store names
app.get('/', (req, res) => {
// send the names to the template
res.sendFile(path.join(__dirname, './public', 'index.html'));
});
app.post('/add-time', (req, res) => {
const time = req.body.time;
const name = req.body.name;
let user = {time: time, name: name};
times.push(user);
res.json(times);
});
// New route to list all names
app.get('/times', (req, res) => {
res.json(times);
});
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});