-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
38 lines (31 loc) · 950 Bytes
/
server.js
File metadata and controls
38 lines (31 loc) · 950 Bytes
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
require('dotenv').config();
const express = require('express');
const mongoose = require('mongoose');
const alumniRouter = require('./routes/Alumni');
const path = require('path');
const app = express();
const PORT = 8081;
app.use(express.json());
// Serve React build from dist/
app.use(express.static('dist'));
// API routes
app.use('/api', alumniRouter);
// Fallback to index.html for React - must come after static and API routes
app.use((req, res) => {
if (!req.path.startsWith('/api')) {
res.sendFile(path.join(__dirname, 'dist', 'index.html'));
}
});
const dbHost = process.env.DATABASE_HOST || '127.0.0.1';
mongoose
.connect(`mongodb://${dbHost}:27017/sce_linkedin`)
.then(() => {
console.log('Connected to MongoDB');
app.listen(PORT, () => {
console.log(`Server listening on port ${PORT}`);
});
})
.catch((err) => {
console.error('Failed to connect to MongoDB:', err);
process.exit(1);
});