Spaces:
Sleeping
Sleeping
Mohamed Abu Basith
commited on
Commit
·
02a1b88
1
Parent(s):
f003230
changes
Browse files
server.js
CHANGED
@@ -1,11 +1,29 @@
|
|
1 |
const http = require('http');
|
|
|
2 |
|
3 |
const PORT = process.env.PORT || 7860;
|
4 |
|
5 |
const requestHandler = (req, res) => {
|
6 |
-
|
7 |
-
|
8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
};
|
10 |
|
11 |
const server = http.createServer(requestHandler);
|
|
|
1 |
const http = require('http');
|
2 |
+
const url = require('url');
|
3 |
|
4 |
const PORT = process.env.PORT || 7860;
|
5 |
|
6 |
const requestHandler = (req, res) => {
|
7 |
+
const parsedUrl = url.parse(req.url, true);
|
8 |
+
|
9 |
+
// If the request is to /api and has ?raw=true, return a raw JSON response.
|
10 |
+
if (parsedUrl.pathname === '/api' && parsedUrl.query.raw === 'true') {
|
11 |
+
res.writeHead(200, { 'Content-Type': 'application/json' });
|
12 |
+
res.end(JSON.stringify({ message: 'Hello from your Node.js API!' }));
|
13 |
+
return;
|
14 |
+
}
|
15 |
+
|
16 |
+
// Default route returns a basic HTML page.
|
17 |
+
res.writeHead(200, { 'Content-Type': 'text/html' });
|
18 |
+
res.end(`
|
19 |
+
<html>
|
20 |
+
<head><title>Welcome</title></head>
|
21 |
+
<body>
|
22 |
+
<h1>Welcome to my HF Space!</h1>
|
23 |
+
<p>For a raw API response, use <code>/api?raw=true</code></p>
|
24 |
+
</body>
|
25 |
+
</html>
|
26 |
+
`);
|
27 |
};
|
28 |
|
29 |
const server = http.createServer(requestHandler);
|