-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
41 lines (31 loc) · 1.06 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
const express = require("express");
const bodyParser = require('body-parser');
const fs = require('fs');
const cors = require('cors');
const path = require('path');
const app = express();
const port = process.env.PORT ;
app.use(bodyParser.json());
app.use(cors());
// Get the absolute path to the emails.json file
const emailsFilePath = path.join(__dirname, 'emails.json');
app.post('/store-email', (req, res) => {
const { email } = req.body;
if (!email) {
return res.status(400).json({ error: 'Email is required' });
}
let existingEmails = [];
try {
existingEmails = JSON.parse(fs.readFileSync(emailsFilePath, 'utf8'));
} catch (error) {
// If the file does not exist or is invalid JSON, ignore and create an empty array
}
// Add the new email to the existing ones
existingEmails.push(email);
// Write the updated emails back to the file
fs.writeFileSync(emailsFilePath, JSON.stringify(existingEmails, null, 2), 'utf8');
res.json({ success: true });
});
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});