forked from VitorCarvalho67/Boot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
44 lines (37 loc) · 1.12 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import "express-async-errors";
import express, { Request, Response, NextFunction } from 'express';
import cors from 'cors';
import { routes } from "./router";
import { AppError } from './errors/error';
import { Server } from 'socket.io';
import http from 'http';
import appSocket from "./connection/socket";
const app = express();
const port = 3333;
app.use(cors());
app.use(express.json());
app.use(routes);
app.use((err: Error, request: Request, response: Response, next: NextFunction) => {
if (err instanceof AppError) {
response.status(err.statusCode).json({
status: "error",
message: err.message
});
} else {
response.status(500).json({
status: "error",
message: `Internal server error - ${err.message}`
});
}
});
const httpServer = http.createServer(app);
const io = new Server(httpServer, {
cors: {
origin: "http://localhost:5173",
methods: ["GET", "POST"]
}
});
appSocket(io);
httpServer.listen(port, () => {
console.log(`Server is running on port ${port} 🚀\nDatabase URL: ${process.env.DATABASE_URL}`);
});