-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
48 lines (39 loc) · 1.36 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
48
const express = require("express");
const User = require("./src/models/user");
const Blog = require("./src/models/blog");
const cookieParser = require('cookie-parser')
const ExpressError = require("./utils/expressError");
const catchAsync = require("./utils/catchAsync");
const blogRoutes = require('./src/routes/blogs')
const homeRoutes = require('./src/routes/home')
const authRouter = require('./src/routes/auth');
const passport = require("passport");
const addUserToRequestBody = require('./utils/confirmUser');
require('./authentication/auth')
const app = express();
//middlewares
app.use(express.json())
app.use(express.urlencoded({ extended: true }));
app.use(cookieParser());
app.use(passport.initialize())
//routes
app.use(addUserToRequestBody);
app.use('/home',homeRoutes)
app.use('/', authRouter)
app.use('/blog',passport.authenticate('jwt', {session:false}), blogRoutes)
app.all("*", (req, res, next) => {
next(new ExpressError("Page not found", 401));
});
const handleValidationErr = (err) => {
return new ExpressError(`Validation Failed...${err.message}`, 400);
};
app.use((err, req, res, next) => {
if (err.name === "ValidationError") err = handleValidationErr(err);
next(err);
});
app.use((err, req, res, next) => {
const { status = 500, message = "something went wrong" } = err;
res.status(status).send(message);
});
//export app
module.exports = app