eletrotupi / tcc/ api/src/createApp.ts81ea07ae23bee8fabf5e0d87cf9cb89507a00384
955 B | 38 lines Raw
1
import express from 'express';
2
import cors from 'cors';
3
import bodyParser from 'body-parser';
4
import morgan from 'morgan';
5
import userRouter from '@app/routes/users';
6
import authRouter from '@app/routes/auth';
7
import { errorHandler } from '@app/middleware/errorHandler';
8
import { PrismaClient } from '@prisma/client';
9

10
export function createApp(prismaClient?: PrismaClient) {
11
  const app = express();
12

13
  // Configure CORS
14
  app.use(cors({
15
    origin: process.env.FRONTEND_URL || '*',
16
    credentials: true
17
  }));
18

19
  app.use(express.json());
20
  app.use(bodyParser.json());
21

22
  // Only use morgan in non-test environments
23
  if (process.env.NODE_ENV !== 'test') {
24
    app.use(morgan('combined'));
25
  }
26

27
  // Make prisma client available to routes if provided (for testing)
28
  if (prismaClient) {
29
    app.locals.prisma = prismaClient;
30
  }
31

32
  app.use("/users", userRouter);
33
  app.use("/auth", authRouter);
34

35
  app.use(errorHandler); // always last
36

37
  return app;
38
}