-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserve.js
More file actions
72 lines (64 loc) · 2.29 KB
/
serve.js
File metadata and controls
72 lines (64 loc) · 2.29 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env node
/**
* Simple HTTP server for local development
* This helps view the app with proper CSS and assets loading
*/
const http = require('http');
const fs = require('fs');
const path = require('path');
const url = require('url');
const PORT = 8080;
const MIME_TYPES = {
'.html': 'text/html',
'.css': 'text/css',
'.js': 'text/javascript',
'.json': 'application/json',
'.png': 'image/png',
'.jpg': 'image/jpeg',
'.gif': 'image/gif',
'.svg': 'image/svg+xml',
'.ico': 'image/x-icon'
};
const server = http.createServer((req, res) => {
const parsedUrl = url.parse(req.url);
let pathname = parsedUrl.pathname;
// Default to index.html
if (pathname === '/') {
pathname = '/index.html';
}
const filePath = path.join(__dirname, pathname);
const extname = path.extname(filePath);
const contentType = MIME_TYPES[extname] || 'text/plain';
fs.readFile(filePath, (err, content) => {
if (err) {
if (err.code === 'ENOENT') {
res.writeHead(404);
res.end('File not found');
} else {
res.writeHead(500);
res.end('Server error');
}
} else {
res.writeHead(200, { 'Content-Type': contentType });
res.end(content);
}
});
});
server.listen(PORT, () => {
console.log(`
╔════════════════════════════════════════════════════╗
║ ║
║ 🚀 OpenCollab Development Server ║
║ ║
║ Server running at: ║
║ http://localhost:${PORT} ║
║ ║
║ Pages: ║
║ • Landing Page: http://localhost:${PORT}/ ║
║ • Application: http://localhost:${PORT}/app.html ║
║ ║
║ Press Ctrl+C to stop ║
║ ║
╚════════════════════════════════════════════════════╝
`);
});