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.
Files changed (2) hide show
  1. package/index.js +43 -0
  2. 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
@@ -0,0 +1,13 @@
1
+ {
2
+ "name": "zerra-core",
3
+ "version": "1.0.0",
4
+ "description": "",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "echo \"Error: no test specified\" && exit 1"
8
+ },
9
+ "keywords": [],
10
+ "author": "",
11
+ "license": "ISC",
12
+ "type": "commonjs"
13
+ }