-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
54 lines (44 loc) · 1.37 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
53
54
// modules
const express = require("express");
const path = require("path");
const logger = require("morgan");
const mongoose = require("mongoose");
const helmet = require("helmet");
// routes files
const usersApiRouter = require("./routes/api/users");
const adminsApiRouter = require("./routes/api/admins");
const quizsetApiRouter = require("./routes/api/quizset");
const quizzesApiRouter = require("./routes/api/quizzes");
const app = express();
// environmental variable
require('dotenv').config()
// helmet for Protection
app.use(helmet())
// middleware
app.use(logger("dev"));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, "public")));
// connecting to mongodb
mongoose.connect(
process.env.ATLAS,
{ useNewUrlParser: true, useUnifiedTopology: true },
err => {
console.log(err ? err : "mongoDB connected");
}
);
mongoose.set('useFindAndModify', false);
// routes
app.use("/api/v1/quizzes", quizzesApiRouter);
app.use("/api/v1/admins", adminsApiRouter);
app.use("/api/v1/users", usersApiRouter);
app.use("/api/v1/quizset", quizsetApiRouter);
// sending index.html from public
app.get('*', (req, res, next) => {
res.sendFile(path.resolve(__dirname, "public/index.html"))
});
// error handlers
app.use((err, req, res, next) => {
res.status(500).json({ success: false, err });
});
module.exports = app;