-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
42 lines (31 loc) · 963 Bytes
/
index.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
import express from 'express'
import mongoose from 'mongoose'
import 'dotenv/config'
import router from './config/router.js'
const app = express()
const startServer = async () => {
try {
//parse incoming requests with JSON payloads
app.use(express.json())
//establish a connection to a MongoDB database
await mongoose.connect(process.env.MONGO_URI)
// capture incoming requests
app.use((req, res, next) => {
console.log(`Incoming request: ${req.method} ${req.url}`)
next()
})
// integrate the defined routes
app.use('/api', router)
// middleware for handling routes not found
app.use((req, res) => {
return res.status(404).json({ message: 'Route not found' })
})
// listen to server
app.listen(process.env.PORT, () => {
console.log(`Server up and running on port ${process.env.PORT}`)
})
} catch (error) {
console.log(error.message)
}
}
startServer()