-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
32 lines (26 loc) · 904 Bytes
/
server.js
File metadata and controls
32 lines (26 loc) · 904 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
const express = require('express');
const fs = require('fs');
const path = require('path');
const bodyParser = require('body-parser');
const app = express();
const PORT = 3000;
// Middleware to parse JSON
app.use(bodyParser.json());
// Serve static files (e.g., your HTML, CSS, and JS files)
app.use(express.static(path.join(__dirname)));
// Endpoint to save JSON data
app.post('/save-json', (req, res) => {
const jsonData = req.body;
// Write the JSON data to test.json
fs.writeFile(path.join(__dirname, 'test.json'), JSON.stringify(jsonData, null, 2), (err) => {
if (err) {
console.error('Error saving JSON:', err);
return res.status(500).send('Error saving JSON');
}
res.send('JSON data saved successfully');
});
});
// Start the server
app.listen(PORT, () => {
console.log(`Server is running at http://localhost:${PORT}`);
});