-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
73 lines (58 loc) · 1.77 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
import express from "express";
import bodyParser from "body-parser";
import cors from "cors";
import dotenv from "dotenv";
// Import model & seeder
import dbConfig from "./src/config/dbConfig.js";
import db from "./src/models/index.js";
import authRouter from "./routes/authRoutes.js";
import userRouter from "./routes/userRoutes.js";
import categoryRouter from "./routes/categoryRoutes.js";
import postRouter from "./routes/postRoutes.js";
import commentRouter from "./routes/commentRoutes.js";
// import developmentRouter from "./routes/development.routes.js";
if (process.env.NODE_ENV != "production") {
dotenv.config();
}
const app = express();
app.use(
cors({
origin: true,
})
);
app.use("/static", express.static("public")); //to access the files in public folder
app.use(bodyParser.json());
app.use((req, res, next) => {
res.setHeader(
"Access-Control-Allow-Headers",
"x-access-token, Origin, Content-Type, Accept"
);
next();
}); // to use header
// Define route
app.use("/api/auth", authRouter);
app.use("/api/users", userRouter);
app.use("/api/categories", categoryRouter);
app.use("/api/posts", postRouter);
app.use("/api/comments", commentRouter);
// app.use("/development", developmentRouter);
db.mongoose
.connect(dbConfig(), {
useCreateIndex: true,
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false,
})
.then(() => {
console.log("Successfully connect to MongoDB.");
})
.catch((err) => {
console.error("Connection error", err);
process.exit();
});
// PORT define
const PORT = process.env.PORT || process.env.APP_PORT;
app.listen(PORT, () => {
console.log(`Server is running on port: ${PORT}`);
});
export default app;