-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
72 lines (64 loc) · 1.79 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
72
const express = require("express");
const morgan = require("morgan");
const bodyParser = require("body-parser");
const cookieParser = require("cookie-parser");
const cors = require("cors");
const mongoose = require("mongoose");
require("dotenv").config();
// bring routes
const blogRoutes = require("./routes/blog");
const authRoutes = require("./routes/auth");
const userRoutes = require("./routes/user");
const categoryRoutes = require("./routes/category");
const tagRoutes = require("./routes/tag");
const formRoutes = require("./routes/form");
// app
const app = express();
const sitemapOptions = {
root: __dirname,
headers: {
'Content-Type': 'text/xml;charset=UTF-8'
}
};
// db
mongoose
.connect(process.env.DATABASE_LOCAL, {
useNewUrlParser: true,
useCreateIndex: true,
useFindAndModify: false,
useUnifiedTopology: true
})
.then(() => console.log("[+] Database connected."))
.catch(err => {
console.log(err);
});
// middlewares
app.use(express.static('public'));
app.use(cors());
app.use(morgan("dev"));
// added to fix PayloadTooLargeError
app.use(bodyParser.json({limit: "50mb"}));
app.use(bodyParser.urlencoded({limit: "50mb",
extended: true,
parameterLimit: 50000
}));
// app.use(bodyParser.json());
app.use(cookieParser());
// cors
if(process.env.NODE_ENV === "development") {
app.use(cors({origin: `${process.env.CLIENT_URL}`}));
};
// routes middleware
app.use("/api", blogRoutes);
app.use("/api", authRoutes);
app.use("/api", userRoutes);
app.use("/api", categoryRoutes);
app.use("/api", tagRoutes);
app.use("/api", formRoutes);
// serve sitemap
app.get('/sitemap.xml', (req, res) => res.status(200).sendFile('sitemap.xml', sitemapOptions));
// port
const port = process.env.PORT || 8000;
app.listen(port, () => {
console.log(`[+] Server is running on port ${port}.`);
});