-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
40 lines (33 loc) · 1.31 KB
/
index.ts
File metadata and controls
40 lines (33 loc) · 1.31 KB
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
import { IncomingMessage, ServerResponse, createServer } from 'http';
import authRoute from './src/features/auth/route';
import log from './src/utils/logger';
import { PORT } from './config/constants';
import spotsRoute from './src/features/spots/route';
import reservationsRoute from './src/features/reservations/reservations.route';
import paymentsRoute from './src/features/payments/payments.route';
import { notFoundHandler } from './src/utils/http';
import garageRoute from './src/features/garage/route';
const server = createServer((req: IncomingMessage, res: ServerResponse) => {
res.setHeader('Content-Type', 'application/json');
// Log the incoming request
const loggedMessage = `${req.method} ${req.url}`;
console.log(loggedMessage);
log(loggedMessage);
const routes = [
{ url: "/api/auth", handler: authRoute },
{ url: "/api/reservations", handler: reservationsRoute },
{ url: "/api/payments", handler: paymentsRoute },
{ url: "/api/garages", handler: garageRoute },
{ url: "/api/spots", handler: spotsRoute },
];
const matchedRoute = routes.find((route) => req.url?.startsWith(route.url));
if (matchedRoute) {
matchedRoute.handler(req, res);
}
else {
notFoundHandler(req, res);
}
});
server.listen(PORT, () => {
console.log(`Server is running on PORT ${PORT}`);
})