-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
51 lines (39 loc) · 1.5 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
const express = require('express');
const mongoose = require('mongoose');
const cors = require('cors');
require('dotenv').config();
const path = require("path");
const app = express();
// Import API routes
const clueRoutes = require('./routes/api/clues');
const authorRoutes = require('./routes/api/authors');
const editorRoutes = require('./routes/api/editors');
// Connect to MongoDB
// remote mongodb+srv://whenHMK:<password>@whmk.3sd5shf.mongodb.net/?retryWrites=true&w=majority
//local 'mongodb://localhost:27017/mern-demo'
mongoose.connect( process.env.MONGODB_CONNECTION_STRING, { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => console.log('Connected to MongoDB'))
.catch(err => console.error('Could not connect to MongoDB:', err));
// Apply CORS middleware
app.use(cors());
app.use(express.static(path.resolve(__dirname, "client/build")));
// Middleware for parsing JSON
app.use(express.json());
// Serve static files from the React app
app.use(express.static(path.resolve(__dirname, 'client/build')));
// Use API routes
app.use('/api/clues', clueRoutes);
app.use('/api/authors', authorRoutes);
app.use('/api/editors', editorRoutes);
// Simple "Hello, World!" route
app.get('/api/hello', (req, res) => {
res.json({ message: 'Hello, World!' });
});
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'client/build', 'index.html'));
});
// Start the server
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});