-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
42 lines (31 loc) · 1.05 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
import express from 'express';
import bodyParser from 'body-parser';
import fs from 'fs';
import path from 'path';
import cors from 'cors';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const app = express();
const PORT = 3000;
app.use(cors());
app.use(bodyParser.json());
app.post('/submit', (req, res) => {
const { name, email, message } = req.body;
if (!name || !email || !message) {
return res.status(400).json({ error: 'All fields are required!' });
}
const data = { name, email, message };
const jsonData = JSON.stringify(data, null, 2);
const filePath = path.join(__dirname, 'submissions.json');
fs.writeFile(filePath, jsonData, (err) => {
if (err) {
console.error('Error writing to file:', err);
return res.status(500).json({ error: 'Failed to save message' });
}
res.status(200).json({ success: 'Message saved successfully!' });
});
});
app.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`);
});