-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
67 lines (56 loc) · 1.99 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
const express = require('express');
const cors = require('cors');
const bodyParser = require('body-parser');
const fs = require('fs').promises;
const path = require('path');
const app = express();
const port = 3000;
// Middleware
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// Ensure emails directory exists
const emailsDir = path.join(__dirname, 'data');
const emailsFile = path.join(emailsDir, 'subscribers.json');
async function ensureDirectoryExists() {
try {
await fs.mkdir(emailsDir, { recursive: true });
try {
await fs.access(emailsFile);
} catch {
await fs.writeFile(emailsFile, JSON.stringify([]));
}
} catch (error) {
console.error('Error setting up data directory:', error);
}
}
// Route to handle email submissions
app.post('/subscribe', async (req, res) => {
try {
const { email } = req.body;
// Basic email validation
if (!email || !email.includes('@')) {
return res.status(400).json({ error: 'Invalid email address' });
}
// Read existing emails
const data = await fs.readFile(emailsFile, 'utf8');
const emails = JSON.parse(data);
// Check if email already exists
if (emails.includes(email)) {
return res.status(409).json({ error: 'Email already subscribed' });
}
// Add new email
emails.push(email);
await fs.writeFile(emailsFile, JSON.stringify(emails, null, 2));
res.status(200).json({ message: 'Subscription successful' });
} catch (error) {
console.error('Error handling subscription:', error);
res.status(500).json({ error: 'Server error' });
}
});
// Initialize directory and start server
ensureDirectoryExists().then(() => {
app.listen(port, () => {
console.log(`Server running on http://localhost:${port}`);
});
});