-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
executable file
·52 lines (50 loc) · 1.42 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
49
50
51
52
import express from "express";
import bodyParser from "body-parser";
import passport from "passport";
import path from "path";
import cors from "cors";
import dotenv from "dotenv";
import passportAuth from "./server/config/passport";
//@router
import authRoutes from "./server/router/api/authRoute";
import userRoutes from "./server/router/api/userRoutes";
dotenv.config();
//@express server
const app = express();
//@cors middleware
app.use(cors());
//@bodyParser
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
//@static folder
app.use("/images/", express.static(path.join(__dirname, "server/public/uploads")));
//app.use(express.static(path.join(__dirname, "server/public")));
app.use(express.static(path.join(__dirname, "frontend")));
//@router configuration
app.use("/api/v1/", authRoutes);
app.use("/api/v1/", userRoutes);
//@redirect all traffic to frontend directory
app.use((req, res) => {
res.sendFile(path.join(__dirname, "frontend/index.html"));
});
//@passport middleware
app.use(passport.initialize());
passportAuth(passport);
//@Error handling
app.use((req, res, next) => {
const error = new Error("Sorry request not found");
error.status = 404;
next(error);
});
//@handling all kind of error that comes anywhere
app.use((error, req, res, next) => {
res.status(error.status || 500);
res.json({
error: {
message: error.message
}
});
next();
});
//@populate
export default app;