-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
54 lines (42 loc) · 1.2 KB
/
Copy pathserver.js
File metadata and controls
54 lines (42 loc) · 1.2 KB
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
const express = require('express');
const mongoose = require('mongoose');
const morgan = require('morgan');
const path = require('path');
const {
isProduction,
mongoURI,
assertProductionConfig,
} = require('./runtime-config');
const app = express();
app.use(express.json());
app.use(morgan('dev'));
assertProductionConfig();
mongoose
.connect(mongoURI, {
serverSelectionTimeoutMS: 10000,
})
.then(() => {
console.log('MongoDB database connection established successfully');
})
.catch((error) => {
console.error('MongoDB connection failed:', error.message);
});
app.use('/api/items', require('./routes/api/items'));
app.use('/api/users', require('./routes/api/users'));
app.use('/api/auth', require('./routes/api/auth'));
if (isProduction) {
const staticDirectory = process.env.VERCEL
? path.join(__dirname, 'public')
: path.join(__dirname, 'client', 'dist');
app.use(express.static(staticDirectory));
app.get('*', (req, res) => {
res.sendFile(path.join(staticDirectory, 'index.html'));
});
}
const port = process.env.PORT || 5050;
if (!process.env.VERCEL) {
app.listen(port, () => {
console.log(`Server is running on port: ${port}`);
});
}
module.exports = app;