-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
32 lines (29 loc) · 875 Bytes
/
app.js
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
import express from 'express';
import dbConnection from './dbConnection.js';
import routes from './routes.js';
const app = express();
const port = process.env.PORT || 3000;
// Middleware to parse JSON requests
app.use(express.json());
app.get('/', (req, res) => {
res.send('Hello World!');
});
app.use('/api', routes);
app.use((err, req, res, next) => {
err.statusCode = err.statusCode || 500;
err.message = err.message || 'Internal Server Error';
res.status(err.statusCode).json({
message: err.message,
});
});
// If database is connected successfully, then run the server
dbConnection
.getConnection()
.then(() => {
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});
})
.catch((err) => {
console.log(`Failed to connect to the database: ${err.message}`);
});