Skip to content
This repository has been archived by the owner on Aug 16, 2024. It is now read-only.

fix/crashing #3

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 28 additions & 6 deletions src/node/start.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
import fs from 'node:fs'
import { join } from 'node:path'
import { cwd } from 'node:process'
import {join} from 'node:path'
import {cwd} from 'node:process'
import type {NextFunction, Request, Response} from 'express'
import express from 'express'
import devalue from '@nuxt/devalue'
import cookieParser from 'cookie-parser'
import { load } from 'cheerio'
import { type HeadTag } from '@vueuse/head'
import {load} from 'cheerio'
import {type HeadTag} from '@vueuse/head'

const catchAsync = (fn: (req: Request, res: Response, next: NextFunction) => Promise<void>) =>
(req: Request, res: Response, next: NextFunction) => {
Promise.resolve(fn(req, res, next)).catch(next)
}

process.on('unhandledRejection', (reason, promise) => {
console.error('Unhandled Rejection at:', promise, 'reason:', reason)
// Additional logic as needed
})

process.on('uncaughtException', (error: Error) => {
console.error('Uncaught Exception:', error)
// Additional logic as needed
})

export async function start(port: number, hostname: string) {
const template = fs.readFileSync(join(cwd(), 'dist/client/index.html'), 'utf-8')
Expand All @@ -20,8 +36,9 @@ export async function start(port: number, hostname: string) {
index: false,
}))
app.use(cookieParser())
app.use('*', async (req, res) => {
app.use('*', catchAsync(async (req, res, next) => {
const url = req.originalUrl
console.log(`Requested: ${url}`)

const generateApp = (await import(join(cwd(), 'dist/server/index.js'))).generateApp

Expand Down Expand Up @@ -92,7 +109,12 @@ export async function start(port: number, hostname: string) {
$('body').append(`<div id="teleports">${teleports['#teleports']}</div>`)
}

res.status(200).set({ 'Content-Type': 'text/html' }).end($.html())
res.status(200).set({'Content-Type': 'text/html'}).end($.html())
}))

app.use((error: Error, req: Request, res: Response, next: NextFunction) => {
console.error('Unhandled Error:', error)
res.status(500).send('Internal Server Error')
})

app.listen(port, hostname, () => {
Expand Down