-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.ts
49 lines (35 loc) · 1.34 KB
/
server.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
45
46
47
48
49
import express, { NextFunction, Request, Response } from 'express';
import dotenv from 'dotenv';
import renderHeaders from './controllers/renderHeaders';
import userRoutes from './routes/userRoutes';
import songsRoutes from './routes/songsRoutes';
import userLibraryRoutes from './routes/userLibraryRoutes';
import logErrorMiddleware from './middleware/logErrorMiddleware';
const path = require('path');
dotenv.config();
let port = typeof process.env.PORT === 'string' && parseFloat(process.env.PORT);
const app = express();
app.use(express.json());
app.use((req, res, next) => renderHeaders(req, res, next));
app.use('/api/user', userRoutes);
app.use('/api/songs', songsRoutes);
app.use('/api/user-library', userLibraryRoutes);
if (port) {
app.use(express.static(path.join(__dirname, '..', 'frontend', 'dist')));
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, '..', 'frontend', 'dist', 'index.html'));
});
}
app.use(logErrorMiddleware);
app.use((err: any, req: Request, res: Response, next: NextFunction) => {
if (err.response?.status === 400) {
res
.status(400)
.send({ error: err.response.data.error.message, status: 400 });
console.log('====================== status 400');
}
});
if (typeof port !== 'number') {
port = 3000;
}
app.listen(port, () => console.log(`Server is running on port ${port}`));