-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
47 lines (38 loc) · 1.41 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
const express = require('express');
const cors = require('cors');
require('dotenv').config();
const app = express();
// Middleware for parsing application/json
app.use(express.json());
const allowedOrigins = ['https://www.utahgolfguru.com', 'http://localhost:3000', 'https://api.utahgolfguru.com'];
app.use(cors({
origin: (origin, callback) => {
// allow requests with no origin - like mobile apps or curl requests
if (!origin) return callback(null, true);
if (allowedOrigins.indexOf(origin) === -1) {
const msg = 'The CORS policy for this site does not allow access from the specified origin.';
return callback(new Error(msg), false);
}
return callback(null, true);
},
methods: 'GET, POST, PUT, DELETE, OPTIONS',
allowedHeaders: 'Content-Type, Authorization',
credentials: true
}));
app.use((err, req, res, next) => {
console.error('Unhandled error:', err);
res.status(500).send('Internal Server Error');
});
// Routes setup
const userRoutes = require('./routes/users');
const coursesRoutes = require('./routes/courses');
const authRoutes = require('./routes/auth');
// Apply user, courses, and auth routes
app.use('/users', userRoutes);
app.use('/courses', coursesRoutes);
app.use('/auth', authRoutes);
const port = process.env.PORT || 3001;
app.listen(port, () => {
console.log(`Server listening on port ${port}`);
});
module.exports = app;