| | import http from 'http' |
| |
|
| | import tcpPortUsed from 'tcp-port-used' |
| | import dotenv from 'dotenv' |
| |
|
| | import { checkNodeVersion } from './lib/check-node-version' |
| | import '../observability/lib/handle-exceptions' |
| | import createApp from './lib/app' |
| | import warmServer from './lib/warm-server' |
| |
|
| | dotenv.config() |
| |
|
| | checkNodeVersion() |
| |
|
| | const { PORT, NODE_ENV } = process.env |
| | const port = Number(PORT) || 4000 |
| |
|
| | export async function main() { |
| | if (NODE_ENV !== 'production') { |
| | await checkPortAvailability() |
| | } |
| |
|
| | return await startServer() |
| | } |
| |
|
| | async function checkPortAvailability() { |
| | |
| | const portInUse = await tcpPortUsed.check(port) |
| | if (portInUse) { |
| | console.log(`\n\n\nPort ${port} is not available. You may already have a server running.`) |
| | console.log( |
| | `Try running \`npx kill-port ${port}\` to shut down all your running node processes.\n\n\n`, |
| | ) |
| | console.log('\x07') |
| | process.exit(1) |
| | } |
| | } |
| |
|
| | async function startServer() { |
| | const app = createApp() |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | await warmServer([]) |
| |
|
| | |
| | const server = http.createServer(app) |
| |
|
| | return server |
| | .listen(port, () => console.log(`app running on http://localhost:${port}`)) |
| | .on('error', () => server.close()) |
| | } |
| |
|