-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
68 lines (50 loc) · 2.69 KB
/
Copy pathserver.js
File metadata and controls
68 lines (50 loc) · 2.69 KB
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
68
/**
* server.js — Entry point for the Email Deliverability Checker
*
* Starts an Express server, registers middleware and routes.
*/
const express = require("express");
const cors = require("cors");
const rateLimit = require("express-rate-limit");
const verifyRoute = require("./routes/verifyRoute");
const app = express();
const PORT = process.env.PORT || 3000;
// Trust proxy
app.set('trust proxy', 1)
// ── CORS ──────────────────────────────────────────────────────────────────────
const allowedOrigins = process.env.NODE_ENV === "production"
? [process.env.FRONTEND_URL]
: ["http://localhost:5173", "http://localhost:3000"];
app.use(cors({
origin: allowedOrigins,
methods: ["GET", "POST"],
credentials: true,
}));
// ── Middleware ────────────────────────────────────────────────────────────────
// Parse JSON request bodies
app.use(express.json({ limit: "5mb" }));
// ── Rate limiting ─────────────────────────────────────────────────────────────
// Global rate limiter — max 20 requests per IP per 15 minutes
const globalLimiter = rateLimit({
windowMs: 15 * 60 * 1000,
max: 20,
standardHeaders: true,
legacyHeaders: false,
message: {
error: "Too many requests from this IP. Please wait 15 minutes and try again.",
},
});
app.use("/verify", globalLimiter);
// ── Routes ────────────────────────────────────────────────────────────────────
app.use("/verify", verifyRoute);
// Health check — confirms the server is running
app.get("/health", (req, res) => res.json({ status: "ok" }));
// ── Global error handler ──────────────────────────────────────────────────────
app.use((err, req, res, next) => {
console.error("[server error]", err.message);
res.status(500).json({ error: "Internal server error." });
});
// ── Start ─────────────────────────────────────────────────────────────────────
app.listen(PORT, () => {
console.log(`\n✅ Email Checker running → http://localhost:${PORT}\n`);
});