-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
68 lines (53 loc) · 1.66 KB
/
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
require('dotenv').config();
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const userAuthRoute = require('./routes/authRoute');
const companyRoute = require('./routes/companyRoute');
const jobRoute = require('./routes/jobRoute');
const walletRoute = require('./routes/walletRoute');
const transactionRoute = require('./routes/transactionRoute');
const connectDB = require('./database/connect');
// Middleware
app.use(express.json());
app.use(bodyParser.urlencoded({ extended: true }));
// Testing Routes
app.get('/', (req, res) => {
res.json({
message: "Hello from Server"
})
});
// Routes auth, company, wallet, job
app.use('/api/v1', userAuthRoute);
app.use('/api/v1', companyRoute);
app.use('/api/v1', walletRoute);
app.use('/api/v1', jobRoute);
app.use('/api/v1', transactionRoute);
// Error handling middleware
app.use((err, req, res, next) => {
console.error(err);
res.status(err.status || 500).json({
error: err.message || 'Internal Server Error',
});
});
// start the server process
const PORT = process.env.PORT || 5000;
// To initialise connectDB function
connectDB();
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});
//Handle uncaught exceptions
process.on("uncaughtException", (err) => {
console.log(`ERROR: ${err.stack}`);
console.log(`Shutting down due to uncaught exception`);
process.exit(1);
});
//Handle unhandled promise rejections
process.on("unhandledRejection", (err) => {
console.log(`ERROR: ${err.message}`);
console.log("Shutting down the server due to unhandled Promise rejection");
server.close(() => {
process.exit(1);
});
});