Initial commit - frontend
This commit is contained in:
46
server.ts
Normal file
46
server.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
// server.ts
|
||||
import https from 'https';
|
||||
import fs from 'fs';
|
||||
import { parse } from 'url';
|
||||
import next from 'next';
|
||||
import type { IncomingMessage, ServerResponse } from 'http';
|
||||
|
||||
const dev = process.env.NODE_ENV !== 'production';
|
||||
const app = next({ dev });
|
||||
const handle = app.getRequestHandler();
|
||||
|
||||
const cert = fs.readFileSync('./dev.psg.net.au+4.pem');
|
||||
const key = fs.readFileSync('./dev.psg.net.au+4-key.pem');
|
||||
|
||||
|
||||
app.prepare().then(() => {
|
||||
const server = https.createServer({ key, cert }, async (req: IncomingMessage, res: ServerResponse) => {
|
||||
const parsedUrl = parse(req.url!, true);
|
||||
|
||||
// Optional: for CORS with /_next/webpack-hmr
|
||||
if (req.url?.startsWith('/_next/webpack-hmr')) {
|
||||
res.setHeader('Access-Control-Allow-Origin', 'https://dev.psg.net.au');
|
||||
res.setHeader('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
|
||||
}
|
||||
|
||||
try {
|
||||
await handle(req, res, parsedUrl);
|
||||
} catch (err) {
|
||||
console.error('❌ Error handling request:', err);
|
||||
res.statusCode = 500;
|
||||
res.end('internal error');
|
||||
}
|
||||
});
|
||||
|
||||
// 🔥 THIS IS CRUCIAL
|
||||
server.on('upgrade', (req, socket, head) => {
|
||||
// Forward the upgrade request to Next.js' internal handler
|
||||
// @ts-ignore - undocumented but works
|
||||
(app as any).getRequestHandler()(req, socket, head);
|
||||
});
|
||||
|
||||
|
||||
server.listen(3000, () => {
|
||||
console.log('✅ HTTPS dev server running at https://localhost:3000');
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user