forked from goitacademy/nodejs-homework-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
36 lines (25 loc) · 966 Bytes
/
app.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
const express = require("express");
const logger = require("morgan");
const cors = require("cors");
const connectDB = require("./config/database");
connectDB();
const app = express();
app.use(express.json());
app.use(express.static("public"));
const contactsRouter = require("./routes/api/contactRoutes");
const authRoutes = require("./routes/api/authRoutes");
const avatarRoutes = require("./routes/api/avatarRoutes");
const authenticateToken = require("./middleware/authenticateToken");
app.use("/api/auth", authRoutes);
app.use("/api/auth", authenticateToken, avatarRoutes);
app.use("/api/contacts", authenticateToken, contactsRouter);
const formatsLogger = app.get("env") === "development" ? "dev" : "short";
app.use(logger(formatsLogger));
app.use(cors());
app.use((req, res) => {
res.status(404).json({ message: "404 Not found" });
});
app.use((err, req, res, next) => {
res.status(500).json({ message: err.message });
});
module.exports = app;