zerra-core 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +43 -0
- package/package.json +13 -0
package/index.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
const http = require("http");
|
|
2
|
+
const fs = require("fs");
|
|
3
|
+
const path = require("path");
|
|
4
|
+
|
|
5
|
+
function startServer(port = 3000) {
|
|
6
|
+
const apiDir = path.join(process.cwd(), "api");
|
|
7
|
+
|
|
8
|
+
const server = http.createServer((req, res) => {
|
|
9
|
+
const { url, method } = req;
|
|
10
|
+
|
|
11
|
+
// Simple path cleaning: remove trailing slashes and get file path
|
|
12
|
+
const cleanPath = url === "/" ? "/index" : url;
|
|
13
|
+
const filePath = path.join(apiDir, `${cleanPath}.js`);
|
|
14
|
+
|
|
15
|
+
if (fs.existsSync(filePath)) {
|
|
16
|
+
try {
|
|
17
|
+
// Clear cache for hot-reloading in dev (optional, but good for DX)
|
|
18
|
+
delete require.cache[require.resolve(filePath)];
|
|
19
|
+
const handler = require(filePath);
|
|
20
|
+
|
|
21
|
+
if (typeof handler === "function") {
|
|
22
|
+
handler(req, res);
|
|
23
|
+
} else {
|
|
24
|
+
res.statusCode = 500;
|
|
25
|
+
res.end(`Error: Handler in ${cleanPath}.js must be a function.`);
|
|
26
|
+
}
|
|
27
|
+
} catch (err) {
|
|
28
|
+
res.statusCode = 500;
|
|
29
|
+
res.end(`Runtime Error: ${err.message}`);
|
|
30
|
+
}
|
|
31
|
+
} else {
|
|
32
|
+
res.statusCode = 404;
|
|
33
|
+
res.end(`Route ${url} not found (No file at ${filePath})`);
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
server.listen(port, () => {
|
|
38
|
+
console.log(`\nš Zerra Engine started on http://localhost:${port}`);
|
|
39
|
+
console.log(`š Mapping routes from: ${apiDir}\n`);
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
module.exports = { startServer };
|
package/package.json
ADDED