-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
35 lines (27 loc) · 1.12 KB
/
Copy pathserver.ts
File metadata and controls
35 lines (27 loc) · 1.12 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
import { readFileSync } from 'fs';
import {} from 'uuid'; // trigger perry-stdlib linking (needed for HTTP server symbols)
declare function js_http_server_create(port: number): number;
declare function js_http_server_accept_v2(server: number): number;
declare function js_http_request_method(req: number): string;
declare function js_http_request_path(req: number): string;
declare function js_http_respond_html(req: number, status: number, body: string): boolean;
declare function js_http_respond_not_found(req: number): boolean;
const PORT = 3000;
const html = readFileSync('./index.html', 'utf8');
const server = js_http_server_create(PORT);
console.log(`Hone landing server running at http://localhost:${PORT}`);
while (true) {
const req = js_http_server_accept_v2(server);
if (req < 0) {
console.log('Server error');
break;
}
const method = js_http_request_method(req);
const path = js_http_request_path(req);
console.log(`${method} ${path}`);
if (method === 'GET' && (path === '/' || path === '/index.html')) {
js_http_respond_html(req, 200, html);
} else {
js_http_respond_not_found(req);
}
}