commit f543f75f7ebb6955ab095a306c5d1e759510a0fc
parent 01d39f7878ac4599b32586834a35e032e9983c17
Author: Amit Dutta <amitdutta4255@gmail.com>
Date: Tue, 26 May 2026 17:59:09 +0530
Add CORS middleware and new /api/emails route
Diffstat:
1 file changed, 23 insertions(+), 0 deletions(-)
diff --git a/server.js b/server.js
@@ -1,6 +1,8 @@
const express = require('express');
const { Resend } = require('resend');
const { Webhook } = require('svix');
+const cors = require('cors'); // <-- ADDED CORS
+const path = require('path'); // <-- ADDED PATH
const app = express();
const port = process.env.PORT || 3000;
@@ -8,6 +10,19 @@ const port = process.env.PORT || 3000;
const resend = new Resend(process.env.RESEND_API_KEY);
const webhookSecret = process.env.RESEND_WEBHOOK_SECRET;
+// --- ADDED MIDDLEWARE & STATIC SERVING ---
+app.use(cors());
+
+// Serve the frontend files from the 'public' directory
+app.use(express.static(path.join(__dirname, 'public')));
+
+// Explicitly serve the service worker at the root scope
+app.get('/sw.js', (req, res) => {
+ res.sendFile(path.join(__dirname, 'public', 'sw.js'));
+});
+// -----------------------------------------
+
+// Existing incoming webhook route
app.post('/api/incoming', express.text({ type: 'application/json' }), async (req, res) => {
try {
const rawBody = req.body;
@@ -248,6 +263,14 @@ app.post('/api/incoming', express.text({ type: 'application/json' }), async (req
}
});
+// --- ADDED /api/emails ROUTE ---
+app.get('/api/emails', async (req, res) => {
+ const { data, error } = await resend.emails.list();
+ if (error) return res.status(500).json({ error: error.message });
+ res.json(data);
+});
+// -------------------------------
+
app.get('/ping', (req, res) => {
res.status(200).send('Server is awake!');
});