-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
50 lines (41 loc) · 1.4 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
import express from "express";
import morgan from "morgan";
import cors from "cors";
import "dotenv/config";
import mongoose from "mongoose";
import swaggerDocument from './swagger.js';
import swaggerUi from 'swagger-ui-express';
import productsRouter from "./routes/productsRouter.js";
import reviewsRouter from "./routes/reviewsRouter.js";
import subscribeRouter from "./routes/subscribeRouter.js";
import orderRouter from "./routes/orderRouter.js";
import promocodesListRouter from "./routes/promocodesListRouter.js";
const app = express();
const { DB_HOST, PORT } = process.env;
app.use(morgan("tiny"));
app.use(cors());
app.use(express.json());
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument));
app.use("/products", productsRouter);
app.use("/reviews", reviewsRouter);
app.use("/subscribe", subscribeRouter);
app.use("/promocodes", promocodesListRouter);
app.use("/order", orderRouter);
app.use((_, res) => {
res.status(404).json({ message: "Route not found" });
});
app.use((err, req, res, next) => {
const { status = 500, message = "Server error" } = err;
res.status(status).json({ message });
});
mongoose.set('strictQuery', true);
mongoose.connect(DB_HOST)
.then(() => {
app.listen(PORT, () => {
console.log("Database connection successful");
});
})
.catch(error => {
console.log(error.message);
process.exit(1);
});