Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

server.ts

47 lines1.1 KB

1import * as http from 'node:http'
2import { createRequestListener } from 'remix/node-fetch-server'
3​
4import { router } from './app/router.ts'
5​
6const port = process.env.PORT ? Number.parseInt(process.env.PORT, 10) : 44100
7const hmrProxyPort = process.env.HMR_PROXY_PORT
8 ? Number.parseInt(process.env.HMR_PROXY_PORT, 10)
9 : null
10​
11const server = http.createServer(
12 createRequestListener(async (request) => {
13 try {
14 return await router.fetch(request)
15 } catch (error) {
16 if (!(request.signal.aborted && error === request.signal.reason)) {
17 console.error(error)
18 }
19 return new Response('Internal Server Error', { status: 500 })
20 }
21 }),
23​
24server.listen(port, '127.0.0.1', () => {
25 if (process.env.REMIX_NODE_HMR) {
26 import('remix/node-hmr/runtime').then((nodeHmr) =>
27 nodeHmr.emitServerReady(),
28 )
29 }
30​
31 console.log(`Server listening on http://localhost:${hmrProxyPort ?? port}`)
32})
33​
34let shuttingDown = false
35​
36function shutdown() {
37 if (shuttingDown) {
38 return
39 }
40​
41 shuttingDown = true
42 server.close(() => process.exit(0))
43 server.closeAllConnections()
45​
46process.on('SIGINT', shutdown)
47process.on('SIGTERM', shutdown)