forked from RamaRaju-personal-org/webapp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
52 lines (39 loc) · 1.5 KB
/
index.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
require('dotenv').config();
const express = require("express");
const bodyParser = require("body-parser");
const app = express();
const logger = require('./logger');
// Your routes and middleware
const mainRoute = require('./api-routes/mainRoute');
const { initializeDatabase } = require('./models/mysql-db-connect');
app.use(bodyParser.json());
app.use('/', mainRoute);
// Logging middleware for incoming requests
app.use((req, res, next) => {
// logger.debug({message: 'Request received', method: req.method, url: req.originalUrl});
next();
});
// Centralized error handling
app.use((err, req, res, next) => {
// logger.error({message: 'Unhandled exception', error: err.message});
res.status(503).json({ error: 'Service Unavailable' });
});
// This function starts the server and is called only when this file is run directly
function startServer() {
const PORT = process.env.PORT || 3307;
initializeDatabase().then(() => {
app.listen(PORT, () => {
logger.info({message: `Server is running on port ${PORT}`});
});
}).catch(error => {
logger.error({message: 'Error initializing database', error: error.toString()});
});
}
// Export the app for testing
module.exports = app;
// The following code checks if this file is the main module and starts the server.
// This is a common pattern for allowing a file to be imported by other modules for testing or other purposes,
// while still being able to run it directly to start the server.
if (require.main === module) {
startServer();
}