-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
158 lines (144 loc) · 4.87 KB
/
server.js
File metadata and controls
158 lines (144 loc) · 4.87 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/**
* server.js — Local Development Server
*
* Simulates Vercel's static file serving and routing.
* Use this to test your site locally before Vercel deployment.
*
* Usage:
* npm install
* npm start
*
* Then open: http://localhost:3000
*/
const http = require("http");
const fs = require("fs");
const path = require("path");
const url = require("url");
const PORT = process.env.PORT || 3000;
const MIME_TYPES = {
".html": "text/html; charset=UTF-8",
".js": "application/javascript; charset=UTF-8",
".css": "text/css; charset=UTF-8",
".json": "application/json",
".png": "image/png",
".jpg": "image/jpeg",
".jpeg": "image/jpeg",
".gif": "image/gif",
".svg": "image/svg+xml",
".ico": "image/x-icon",
".webp": "image/webp",
".wav": "audio/wav",
".mp4": "video/mp4",
".webm": "video/webm",
".woff": "font/woff",
".woff2": "font/woff2",
".ttf": "font/ttf",
".eot": "application/vnd.ms-fontobject",
".otf": "font/otf",
".wasm": "application/wasm",
".csv": "text/csv",
".xlsx": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
".docx":
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
".pdf": "application/pdf",
".txt": "text/plain",
};
/**
* Resolve file path following Vercel's rules:
* 1. If it's a directory, serve index.html
* 2. If no extension, try .html
* 3. Serve the file as-is
*/
function resolvePath(urlPath) {
let filePath = path.join(
process.cwd(),
urlPath === "/" ? "index.html" : urlPath,
);
// Remove trailing slash
if (filePath.endsWith(path.sep) && filePath !== path.sep) {
filePath = filePath.slice(0, -1);
}
// If no extension, try .html
if (!path.extname(filePath)) {
const htmlPath = filePath + ".html";
if (fs.existsSync(htmlPath)) {
return htmlPath;
}
}
// If directory, serve index.html
if (fs.existsSync(filePath) && fs.statSync(filePath).isDirectory()) {
return path.join(filePath, "index.html");
}
return filePath;
}
const server = http.createServer((req, res) => {
const parsedUrl = url.parse(req.url, true);
const pathname = parsedUrl.pathname;
let filePath = resolvePath(pathname);
const extname = String(path.extname(filePath)).toLowerCase();
const contentType = MIME_TYPES[extname] || "application/octet-stream";
// Read and serve the file
fs.readFile(filePath, (error, content) => {
if (error) {
if (error.code === "ENOENT") {
// Try index.html fallback (for SPA routing)
const fallbackPath = path.join(process.cwd(), "index.html");
fs.readFile(fallbackPath, (fallbackError, fallbackContent) => {
if (!fallbackError) {
res.writeHead(200, { "Content-Type": MIME_TYPES[".html"] });
res.end(fallbackContent, "utf-8");
} else {
res.writeHead(404, { "Content-Type": "text/html" });
res.end(`<!DOCTYPE html>
<html>
<head><title>404 Not Found</title></head>
<body>
<h1>⚠️ 404 - File Not Found</h1>
<p>Requested: <code>${pathname}</code></p>
<p>Resolved to: <code>${filePath}</code></p>
<p><a href="/">← Back to Home</a></p>
</body>
</html>`);
}
});
} else {
res.writeHead(500, { "Content-Type": "text/html" });
res.end(`<!DOCTYPE html>
<html>
<head><title>500 Server Error</title></head>
<body>
<h1>❌ 500 - Server Error</h1>
<p>${error.code}: ${error.message}</p>
</body>
</html>`);
}
} else {
res.writeHead(200, {
"Content-Type": contentType,
"Cache-Control":
extname === ".html" ? "no-cache" : "public, max-age=3600",
});
res.end(content, "utf-8");
}
});
});
server.listen(PORT, "0.0.0.0", () => {
console.log("");
console.log("╔════════════════════════════════════════════╗");
console.log("║ 🚀 ExplainGrade Development Server ║");
console.log("╚════════════════════════════════════════════╝");
console.log("");
console.log(` 🌐 Local URL: http://localhost:${PORT}`);
console.log(` 📁 Serving: ${process.cwd()}`);
console.log("");
console.log(" This server simulates Vercel's routing.");
console.log(" Test here before deploying to Vercel.");
console.log("");
console.log(" Press Ctrl+C to stop the server");
console.log("");
});
// Handle graceful shutdown
process.on("SIGINT", () => {
console.log("\n\n 🛑 Server stopped.\n");
process.exit(0);
});