-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
56 lines (45 loc) · 1.6 KB
/
server.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
import express from 'express';
import fs from 'fs';
import cors from 'cors'; // Import the cors package
const app = express();
const port = 3000;
// Enable CORS for all routes
app.use(cors());
// Middleware to parse JSON bodies
app.use(express.json());
app.post('/submit-form', (req, res) => {
const { fullName, email } = req.body;
// Check if fullName and email are provided
if (!fullName || !email) {
return res.status(400).json({ message: 'Full name and email are required.' });
}
// Read the existing data from formData.json
fs.readFile('../my-vue-app/src/assets/formData.json', 'utf8', (err, data) => {
if (err) {
console.error('Error reading file:', err);
return res.status(500).json({ message: 'Error reading formData.json' });
}
// Parse the existing data
let formData;
try {
formData = JSON.parse(data);
} catch (error) {
console.error('Error parsing JSON:', error);
return res.status(500).json({ message: 'Error parsing formData.json' });
}
// Add the new submission to the data
formData.push({ fullName, email });
// Write the updated data back to formData.json
fs.writeFile('../my-vue-app/src/assets/formData.json', JSON.stringify(formData, null, 2), (err) => {
if (err) {
console.error('Error writing file:', err);
return res.status(500).json({ message: 'Error writing to formData.json' });
}
// Send a success response
res.status(200).json({ message: 'Submission received.' });
});
});
});
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});