import express, { Express } from 'express'; import dotenv from 'dotenv' import routes from './routes' import { errorHandler } from './util/errorHandler' import { wsServer } from './routes/proxyRoute'; import compression from 'compression' import { version } from './package.json'; dotenv.config() const app: Express = express(); const port = process.env.PORT app.use(compression({})) app.use(routes) // 404 handler app.use((req, res) => { if (!res.headersSent) { res.status(404).send('404') } }); // handle errors app.use(errorHandler) const server = app.listen(port, () => { console.log(`SafeTwitch v${version} started on port ${port}`) }) server.on('upgrade', (request, socket, head) => { wsServer.handleUpgrade(request, socket, head, socket => { wsServer.emit('connection', socket, request); }); }); // don't crash on unhandledRejection process.on('unhandledRejection', (reason: Error, p) => {});