-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
45 lines (40 loc) · 1.27 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
// import section
require("dotenv").config();
const express = require("express");
const mongoose = require("mongoose");
const morgan = require("morgan");
const helmet = require("helmet");
const compression = require("compression");
const app = express();
//DB connection
mongoose.connect(process.env.MONGO_DB_URI);
mongoose.connection.on("connected", () => {
console.log("DB connected");
});
mongoose.connection.on("error", (err) => {
console.log("mongodb failed with", err);
});
//import routes
const userRoutes = require("./routes/user.routes");
const tagRoutes = require("./routes/tag.routes");
const authRoutes = require("./routes/auth.routes");
const storyRoutes = require("./routes/story.routes");
//middleware
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(morgan(process.env.NODE_ENV === "development" ? "dev" : "combined"));
app.use(helmet());
app.use(compression());
//router middleware
app.use("/users", userRoutes);
app.use("/auth", authRoutes);
app.use("/tags", tagRoutes);
app.use("/stories", storyRoutes);
app.get("/", (req, res) => {
return res.send({ message: "Welcome :))" });
});
//server listening
const port = process.env.PORT || 8000;
app.listen(port, () => {
console.log(`Example app listening on port ${port}`);
});