Skip to content

Commit fabee2a

Browse files
authored
Update VRtraining.js
Enhancements
1 parent a46406f commit fabee2a

File tree

1 file changed

+53
-28
lines changed

1 file changed

+53
-28
lines changed

js/VRtraining.js

Lines changed: 53 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,58 @@
1-
// Import the necessary modules.
2-
import http from 'http';
3-
import { SimpleHTTPRequestHandler } from 'http';
4-
import { TCPServer } from 'net';
5-
import { open } from 'open';
1+
const http = require('http');
2+
const fs = require('fs');
3+
const path = require('path');
4+
const open = require('open');
65

7-
// Define the TrainingHandler class.
8-
class TrainingHandler extends SimpleHTTPRequestHandler {
9-
// Override the do_GET method to handle custom requests.
10-
do_GET(req, res) {
11-
// Check if the requested file is 'demo.html'.
12-
if (req.url === '/demo.html') {
13-
// Serve the 'demo.html' file.
14-
this.sendFile(req, res, 'demo.html');
15-
} else {
16-
// Use the parent class behavior for other requests.
17-
super.do_GET(req, res);
18-
}
19-
}
20-
}
6+
// Define TrainingHandler equivalent functionality
7+
const requestHandler = (req, res) => {
8+
let filePath = '.' + req.url;
9+
if (filePath === './') {
10+
filePath = './demo.html';
11+
}
2112

22-
// Define the port to use for the server.
23-
const port = 8001;
13+
const extname = path.extname(filePath);
14+
let contentType = 'text/html';
15+
switch (extname) {
16+
case '.js':
17+
contentType = 'text/javascript';
18+
break;
19+
case '.css':
20+
contentType = 'text/css';
21+
break;
22+
case '.png':
23+
contentType = 'image/png';
24+
break;
25+
}
2426

25-
// Create the server and start listening on the specified port.
26-
const server = http.createServer(TrainingHandler);
27-
server.listen(port);
27+
fs.readFile(filePath, (err, content) => {
28+
if (err) {
29+
if (err.code === 'ENOENT') {
30+
res.writeHead(404);
31+
res.end('File not found');
32+
} else {
33+
res.writeHead(500);
34+
res.end('Server error');
35+
}
36+
} else {
37+
res.writeHead(200, { 'Content-Type': contentType });
38+
res.end(content, 'utf-8');
39+
}
40+
});
41+
};
2842

29-
// Print a message to the console indicating that the server is running.
30-
console.log(`Serving VR training at http://localhost:${port}`);
43+
// Create and start server
44+
const server = http.createServer(requestHandler);
45+
const PORT = 8001;
3146

32-
// Open a new tab in the default browser to access the 'demo.html' page.
33-
open(`http://localhost:${port}/demo.html`);
47+
server.listen(PORT, (err) => {
48+
if (err) {
49+
return console.error('Error starting server:', err);
50+
}
51+
52+
console.log(`Serving VR training at http://localhost:${PORT}`);
53+
54+
// Open browser tab
55+
open(`http://localhost:${PORT}/demo.html`).catch(err => {
56+
console.error('Error opening browser:', err);
57+
});
58+
});

0 commit comments

Comments
 (0)