Spaces:
Paused
Paused
Update server.js
Browse files
server.js
CHANGED
|
@@ -15,18 +15,31 @@ async function loadModel() {
|
|
| 15 |
}
|
| 16 |
|
| 17 |
const server = http.createServer(async (req, res) => {
|
| 18 |
-
//
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
res.setHeader('Content-Type', 'application/json');
|
| 20 |
|
| 21 |
-
//
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
| 23 |
res.writeHead(200);
|
| 24 |
res.end(JSON.stringify({ "status": " Backend is running" }));
|
| 25 |
return;
|
| 26 |
}
|
| 27 |
|
| 28 |
-
//
|
| 29 |
-
if (
|
| 30 |
let body = '';
|
| 31 |
req.on('data', chunk => { body += chunk.toString(); });
|
| 32 |
req.on('end', async () => {
|
|
@@ -55,7 +68,7 @@ const server = http.createServer(async (req, res) => {
|
|
| 55 |
|
| 56 |
// Default 404
|
| 57 |
res.writeHead(404);
|
| 58 |
-
res.end(JSON.stringify({ error: "Not Found" }));
|
| 59 |
});
|
| 60 |
|
| 61 |
// Start everything
|
|
|
|
| 15 |
}
|
| 16 |
|
| 17 |
const server = http.createServer(async (req, res) => {
|
| 18 |
+
// 1. Add CORS headers so external websites can talk to this API
|
| 19 |
+
res.setHeader('Access-Control-Allow-Origin', '*');
|
| 20 |
+
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
|
| 21 |
+
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
|
| 22 |
+
|
| 23 |
+
// Handle preflight requests for the API
|
| 24 |
+
if (req.method === 'OPTIONS') {
|
| 25 |
+
res.writeHead(200);
|
| 26 |
+
return res.end();
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
res.setHeader('Content-Type', 'application/json');
|
| 30 |
|
| 31 |
+
// Clean the URL (Removes Hugging Face's ?__theme=light additions)
|
| 32 |
+
const pathname = req.url.split('?')[0];
|
| 33 |
+
|
| 34 |
+
// 2. Your requested Status Check
|
| 35 |
+
if (pathname === '/' && req.method === 'GET') {
|
| 36 |
res.writeHead(200);
|
| 37 |
res.end(JSON.stringify({ "status": " Backend is running" }));
|
| 38 |
return;
|
| 39 |
}
|
| 40 |
|
| 41 |
+
// 3. Code Generation Endpoint
|
| 42 |
+
if (pathname === '/generate' && req.method === 'POST') {
|
| 43 |
let body = '';
|
| 44 |
req.on('data', chunk => { body += chunk.toString(); });
|
| 45 |
req.on('end', async () => {
|
|
|
|
| 68 |
|
| 69 |
// Default 404
|
| 70 |
res.writeHead(404);
|
| 71 |
+
res.end(JSON.stringify({ error: "Not Found", requested_path: pathname }));
|
| 72 |
});
|
| 73 |
|
| 74 |
// Start everything
|