-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
44 lines (39 loc) · 1.27 KB
/
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
37
38
39
40
41
42
43
44
require("express-async-errors");
const express = require("express");
const cors = require("cors"); // before deploying, check pdf
const errorHandler = require("./handlers/errorHandler");
const mongoose = require("mongoose");
const userRoutes = require("./modules/users/users.routes");
const transactionRoutes = require("./modules/transactions/transactions.routes");
require("dotenv").config();
//Connection to MDB DB
mongoose
.connect(process.env.mongo_connection, {})
.then(() => {
console.log("Connection to MongoDB successfull!!");
})
.catch(() => {
console.log("Connection to MongoDB failed!!");
});
require("./models/users.model");
require("./models/transactions.model");
//express
const app = express();
//cors
app.use(cors());
//express to read json instead of xml
app.use(express.json());
//Routes
app.use("/api/users", userRoutes); // userRoutes of folder modules/users will handle /api/users
app.use("/api/transactions", transactionRoutes); // transationRoutes of folder modules/transactions will handle /api/transactions
//endof all routes
app.all("*", (req, res, next) => {
res.status(404).json({
status: "failed",
message: "Error 404 not found",
});
});
app.use(errorHandler);
app.listen(8000, () => {
console.log("Server started successfully");
});