-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
93 lines (82 loc) · 2.59 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
const express = require("express")
require("dotenv").config()
const cookieParser = require("cookie-parser")
const fileUpload = require("express-fileupload")
const morgan = require("morgan")
const app = express()
const mongoose = require("mongoose")
const cors = require("cors")
app.use(cors({
origin: "http://localhost:3000",
credentials: true
}))
// const authenticateSession = require('./middleware/authenticateSession')
// importing routes
const user = require("./routes/userRoutes")
const internalRoutes = require("./routes/internalRoutes")
const openRoutes = require("./routes/openRoutes")
const recruiterRoutes = require("./routes/recruiterRoutes");
const quizRoutes = require('./routes/quizRoutes')
const experience = require("./routes/experience")
const superAdmin = require("./routes/superAdminRoutes")
const admin = require("./routes/adminRoutes")
// regular middlewares
app.use(express.json())
app.use(express.urlencoded({ extended: true }))
//cookie and file middlewares
app.use(cookieParser(process.env.COOKIE_SECRET, { signed: true }))
app.use(
fileUpload({
tempFileDir: "./tmp/",
useTempFiles: true,
})
)
app.use((req, res, next) => {
// res.header("Access-Control-ALlow-Origin", "*")
res.header(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept"
)
res.header("Access-Control-Allow-Origin", "http://localhost:3000")
res.header('Access-Control-Allow-Credentials', 'true');
next()
})
app.use((err, req, res, next) => {
console.log(err)
next()
});
//logging
app.use(morgan("tiny"))
// using routes
app.use("/api/v1", user)
app.use("/api/v1/internal", internalRoutes)
app.use("/api/v1/profile", openRoutes)
app.use("/api/v1/recruiter", recruiterRoutes)
app.use("/api/v1/quiz", quizRoutes)
app.use("/api/v1/experience", experience)
app.use("/api/v1/supadmin", superAdmin)
app.use("/api/v1/admin", admin)
// after all routes
app.get((req, res) => {
res.status(404).json({ message: "Page not found" })
})
// function notFound(req, res) { res.status(404).json({ message: "Page not found" });}
// // If no route is matched by now, it must be a 404
// app.get(notFound).post(notFound).put(notFound).patch(notFound).delete(notFound);
mongoose.set("strictQuery", true)
mongoose.connect(
process.env.MONGO_URI,
{
useNewUrlParser: true,
useUnifiedTopology: true,
},
(err) => {
if (err) {
console.log(err)
} else {
app.listen(process.env.PORT, () => {
console.log(`Server is running on port ${process.env.PORT}`)
})
}
}
);