-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
44 lines (37 loc) · 1.2 KB
/
server.ts
File metadata and controls
44 lines (37 loc) · 1.2 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
41
42
43
44
import express, { Express, Request, Response } from "express";
import cors from 'cors';
import prisma from './prisma/prismaClient'; // Use the Prisma client
import authRoutes from './routes/authRoutes';
import protectedRoutes from './routes/protectedRoutes';
const app: Express = express();
const PORT = 4000;
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(cors());
// Routes
app.use('/api/auth', authRoutes);
app.use('/api', protectedRoutes);
app.get("/", (req: Request, res: Response) => {
res.send("Express + TypeScript Server");
});
app.get('/api', (req, res) => {
res.send('API is running');
});
// Test the Prisma connection and start the server
async function main() {
try {
await prisma.$connect(); // Connecting to the database
console.log('Connection to the database has been established successfully.');
// Start the server
app.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`);
});
} catch (err) {
console.error('Unable to connect to the database:', err);
}
}
main()
.catch(err => console.error('Error:', err))
.finally(async () => {
await prisma.$disconnect(); // Disconnect Prisma when done
});