-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathstatic-serve.js
More file actions
122 lines (106 loc) · 2.95 KB
/
static-serve.js
File metadata and controls
122 lines (106 loc) · 2.95 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/**
* Static file server for Next.js
* This script builds the app once and then serves it statically,
* which is much faster than running the development server.
*/
const http = require('http');
const fs = require('fs');
const path = require('path');
const { exec } = require('child_process');
const PORT = process.env.PORT || 3000;
const BUILD_DIR = path.join(__dirname, '.next');
console.log('🚀 Preparing static server...');
// Check if we need to build
const needsBuild =
!fs.existsSync(BUILD_DIR) ||
!fs.existsSync(path.join(BUILD_DIR, 'server', 'pages'));
if (needsBuild) {
console.log(
'📦 Building project (this will take a moment but only happens once)...',
);
exec('npm run build', (error, stdout, stderr) => {
if (error) {
console.error('❌ Build failed:', error);
return;
}
console.log('✅ Build completed successfully!');
startServer();
});
} else {
console.log('✅ Using existing build');
startServer();
}
function startServer() {
console.log(`🌐 Starting static file server on port ${PORT}...`);
// Simple static file server
const server = http.createServer((req, res) => {
// Strip query parameters
const urlPath = req.url.split('?')[0];
// Default to index.html
let filePath = path.join(
__dirname,
'.next',
'server',
'pages',
urlPath === '/' ? 'index.html' : urlPath,
);
// Add .html extension if no extension
if (!path.extname(filePath)) {
filePath += '.html';
}
// Check if file exists
fs.access(filePath, fs.constants.F_OK, (err) => {
if (err) {
// File not found
res.writeHead(404);
res.end('404 Not Found');
return;
}
// Determine content type
const ext = path.extname(filePath);
let contentType = 'text/html';
switch (ext) {
case '.js':
contentType = 'text/javascript';
break;
case '.css':
contentType = 'text/css';
break;
case '.json':
contentType = 'application/json';
break;
case '.png':
contentType = 'image/png';
break;
case '.jpg':
case '.jpeg':
contentType = 'image/jpeg';
break;
}
// Read and serve the file
fs.readFile(filePath, (err, content) => {
if (err) {
res.writeHead(500);
res.end('500 Server Error');
return;
}
res.writeHead(200, { 'Content-Type': contentType });
res.end(content, 'utf-8');
});
});
});
server.listen(PORT, () => {
console.log(`⭐ Server running at http://localhost:${PORT}/`);
console.log(
'📝 Note: This is a static server, not all dynamic features will work',
);
});
// Handle server shutdown
process.on('SIGINT', () => {
console.log('Shutting down server...');
server.close(() => {
console.log('Server closed');
process.exit(0);
});
});
}