-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
47 lines (41 loc) · 1.56 KB
/
server.js
File metadata and controls
47 lines (41 loc) · 1.56 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
36
37
38
39
40
41
42
43
44
45
46
47
const http = require('http');
const fs = require('fs');
const path = require('path');
const PORT = 3000;
const server = http.createServer((req, res) => {
// Handle the root path by serving the interface.html file
if (req.url === '/' || req.url === '/index.html') {
const filePath = path.join(__dirname, 'interface.html');
fs.readFile(filePath, (err, content) => {
if (err) {
res.writeHead(500);
res.end(`Error loading the interface: ${err.message}`);
return;
}
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(content, 'utf-8');
});
return;
}
// For API endpoints (can be expanded later)
if (req.url.startsWith('/api/')) {
res.writeHead(200, { 'Content-Type': 'application/json' });
// Mock API responses
if (req.url === '/api/blockchain') {
res.end(JSON.stringify({ message: 'Blockchain API endpoint' }));
} else if (req.url === '/api/transactions') {
res.end(JSON.stringify({ message: 'Transactions API endpoint' }));
} else {
res.writeHead(404);
res.end(JSON.stringify({ error: 'API endpoint not found' }));
}
return;
}
// Handle 404 for any other routes
res.writeHead(404);
res.end('Not Found');
});
server.listen(PORT, () => {
console.log(`Server running at http://localhost:${PORT}/`);
console.log(`Open your browser to http://localhost:${PORT}/ to view the blockchain interface`);
});