-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlocal_server.js
42 lines (34 loc) · 1.12 KB
/
local_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
const express = require('express');
const bodyParser = require('body-parser');
const fs = require('fs');
const path = require('path');
const cors = require('cors');
const app = express();
const PORT = 3001;
// Enable CORS for all origins
app.use(cors({
origin: 'http://localhost:3000', // Your frontend URL
}));
// Handle preflight requests
app.options('*', cors());
app.use(bodyParser.json());
// Endpoint to save email
app.post('/save-email', (req, res) => {
const { email } = req.body; // Extract email from request body
if (!email || !email.includes('@')) {
return res.status(400).json({ message: 'Invalid email' });
}
// Save the email to a local file
const filePath = path.join(__dirname, 'emails.txt');
fs.appendFile(filePath, `${email}\n`, (err) => {
if (err) {
console.error('Error saving email:', err);
return res.status(500).json({ message: 'Error saving email' });
}
console.log(`Email saved: ${email}`);
res.status(200).json({ message: 'Email successfully saved!' });
});
});
app.listen(PORT, () => {
console.log(`Local server running at http://localhost:${PORT}`);
});