-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
41 lines (31 loc) · 1.09 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
const express = require('express');
const fs = require('fs');
const path = require('path');
const app = express();
const port = 3000;
// Middleware to parse JSON data from the request body
app.use(express.json());
// Serve the static HTML file
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
// Endpoint to handle form submission and store data in a JSON file
app.post('/submit', (req, res) => {
const userData = req.body;
// Define the path for the JSON file
const filePath = path.join(__dirname, 'data.json');
// Read the existing JSON file (or initialize as an empty array)
let data = [];
if (fs.existsSync(filePath)) {
data = JSON.parse(fs.readFileSync(filePath, 'utf8'));
}
// Add the new user data
data.push(userData);
// Write updated data back to the file
fs.writeFileSync(filePath, JSON.stringify(data, null, 2), 'utf8');
// Send a response back to the client
res.json({ message: 'Data saved successfully!', userData });
});
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});