-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
54 lines (43 loc) · 1.32 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
import express from "express";
import dotenv from "dotenv";
import "express-async-errors";
import path from "path";
import { dirname } from "path";
import { fileURLToPath } from 'url';
dotenv.config();
const app = express();
const __dirname = dirname(fileURLToPath(import.meta.url));
const port = process.env.PORT || 4000;
app.use(express.static(path.resolve(__dirname, "./public")));
app.use(express.json());
//DB Connection
import connect from "./DB/Connect.js";
//Middleware
import notFound from "./ErrorHandler/Not-Found.js";
import ErrorHandler from "./ErrorHandler/errorHandler.js";
//Routes middleware
import authRouter from "./Routes/AuthRoute.js";
import { StatusCodes } from "http-status-codes";
import expenseRouter from "./Routes/ExpenseRoute.js";
import Auth from "./Middlewares/Auth.js";
app.get("/api/v1", (req, res) => {
res.status(StatusCodes.OK).send("Welcome!!");
});
app.use("/api/v1/auth", authRouter);
app.use("/api/v1/expense", Auth, expenseRouter);
app.get("*", (req, res) => {
res.sendFile(path.resolve(__dirname, "./public", "index.html"));
});
app.use(notFound);
app.use(ErrorHandler);
const start = async () => {
try {
await connect(process.env.MONGO_URI);
app.listen(port, () => {
console.log("Server started on port 5000");
});
} catch (error) {
console.log(error);
}
};
start();