-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
44 lines (34 loc) · 1.09 KB
/
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
43
44
import express from "express"
import path from "path"
import ExpressError from "./middleware/ExpressError.js"
import connectDB from "./config/db.js"
import cookieParser from "cookie-parser"
import dotenv from "dotenv"
dotenv.config()
const app = express()
connectDB()
app.use(express.json())
app.use(express.urlencoded({extended:true}))
app.use(cookieParser())
import user from "./MCR/routes/user.js"
import post from "./MCR/routes/post.js"
import review from "./MCR/routes/review.js"
import sessionConfig from "./config/session.js"
// Routes
app.use('/api/user', user)
app.use('/api/post', post)
app.use('/api/review', review)
// Error handling
// Using the custom Error handler for all the possible routes
app.all('*', (req,res,next) => {
next(new ExpressError('Page Not Found, try again.', 404))
});
// Here I'm using app.use() to destructure the -err- parameter
app.use((err, req,res, next) => {
const {status=500, msg = 'something went wrong'} = err;
res.status(status).send(msg)
next()
})
// Running the app
const port = 8000;
app.listen(port, () => console.log(`running, port ${port}`))