-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
49 lines (41 loc) · 1.24 KB
/
server.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
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const session = require('express-session');
const mongodb = require('./db/db');
const fileUpload = require('express-fileupload');
const port = process.env.PORT || 8080;
const app = express();
// Enable CORS for all routes
app.use(cors());
// Express session middleware
app.use(
session({
secret: process.env.SESSION_SECRET || 'default-secret',
resave: false,
saveUninitialized: false
})
);
// Middleware for parsing JSON requests
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(fileUpload());
// Use routes defined in separate files
app.use('/', require('./routes'));
// Error handling middleware
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).send('Something went wrong!');
});
// Initialize the database connection
mongodb.initDb((err) => {
if (err) {
console.error('Error initializing database:', err);
process.exit(1); // Exit the process if DB initialization fails
} else {
app.listen(port, () => {
console.log(`Running and listening on port ${port}`);
console.log('Handcrafted Haven successfully initialized');
});
}
});