generated from Technigo/express-api-starter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
50 lines (43 loc) · 1.4 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
50
import express from 'express'
import cors from 'cors'
import listEndpoints from 'express-list-endpoints'
import mongoose from 'mongoose'
//* Defines the port the app will run on.
const port = process.env.PORT || 8080
const app = express()
//*Import all the routes
import mongoExercisesRoute from './Routes/mongo-exercises'
import mongoUsersRoute from './Routes/mongo-users'
import mongoFavoritesRoute from './Routes/mongo-favorites'
import mongoRecentRoute from './Routes/mongo-recent'
//* Defines the options for CORS
const corsOptions = {
origin: '*',
methods: ['GET', 'POST', 'PATCH', 'DELETE'],
preflightContinue: false, // Enable preflight requests
optionsSuccessStatus: 204, // Return 204 status for successful preflight requests
}
//* Middlewares to enable cors and json body parsing
app.use(cors(corsOptions))
app.use(express.json())
app.options('*', cors())
app.use((req, res, next) => {
if (mongoose.connection.readyState === 1) {
next()
} else {
res.status(503).json({ error: 'Service unavailable' })
}
})
//* Adding the route files to the app
app.use('/', mongoExercisesRoute)
app.use('/', mongoFavoritesRoute)
app.use('/', mongoRecentRoute)
app.use('/', mongoUsersRoute)
//* Start of routes/ Get list of endpoints
app.get('/', (req, res) => {
res.send(listEndpoints(app))
})
//* Start the server
app.listen(port, () => {
console.log(`Server running on http://localhost:${port}`)
})