forked from dev-bhavya/nitj-website-deploy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
71 lines (61 loc) · 1.85 KB
/
server.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
const dotenv = require("dotenv");
dotenv.config({ path: "./.env" });
const mongoose = require("mongoose");
const express = require("express");
const cors = require("cors");
const compression = require("compression");
const mainRouter = require("./routes");
const { admin_panel, router } = require("./admin_panel");
const cookieParser = require("cookie-parser");
const bodyParser = require("body-parser");
const path = require("path");
// process.on("unhandledRejection", (err) => {
// console.log("unhandleed rejection occured");
// console.log(err.name, err.message);
// server.close(() => {
// process.exit(1);
// });
// });
//initialize app
const app = express();
//admin panel
app.use(admin_panel.options.rootPath, router);
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.json({ limit: "5mb" }));
app.use(express.urlencoded({ extended: true }));
app.use(compression());
app.use(cookieParser());
app.use(bodyParser.json({ limit: "5mb" }));
bodyParser.urlencoded({ extended: true });
app.use(express.static(__dirname + "/public"));
app.use("/files", express.static(path.join(__dirname, "..", "nitj_files")));
//allowing all cross origin requests
app.use(
cors({
origin: "*",
})
);
app.use("/api", mainRouter);
app.use("*", (req, res) => {
return res.redirect(`https://v1.nitj.ac.in${req.originalUrl}`);
});
mongoose.set("strictQuery", false);
//server listening------------------------------------------------->
const port = process.env.PORT || 8000;
mongoose
.connect(process.env.URI, {
useNewUrlParser: true,
})
.then(() => {
console.log("connection to database eshtablished");
app.listen(port, () => {
console.log(`Server started on port ${port}`);
});
})
.catch((err) => {
console.log(err);
});
process.on("uncoughtException", (err) => {
console.log(err.name, err.message);
process.exit(1);
});