forked from Richardzk17/bookworm-back-end
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
38 lines (30 loc) · 882 Bytes
/
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
// npm packages
import 'dotenv/config.js'
import express from 'express'
import logger from 'morgan'
import cors from 'cors'
import formData from 'express-form-data'
// connect to MongoDB with mongoose
import './config/database.js'
// import routes
import { router as profilesRouter } from './routes/profiles.js'
import { router as authRouter } from './routes/auth.js'
// create the express app
const app = express()
// basic middleware
app.use(cors())
app.use(logger('dev'))
app.use(express.json())
app.use(formData.parse())
// mount imported routes
app.use('/api/profiles', profilesRouter)
app.use('/api/auth', authRouter)
// handle 404 errors
app.use(function (req, res, next) {
res.status(404).json({ err: 'Not found' })
})
// handle all other errors
app.use(function (err, req, res, next) {
res.status(err.status || 500).json({ err: err.message })
})
export { app }