-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.mjs
48 lines (45 loc) · 1.56 KB
/
server.mjs
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
import express, { json } from "express";
import "dotenv/config";
import cors from "cors";
import { join, dirname } from "path";
import { set, connect } from "mongoose";
const app = express();
// app libraries
import postRoutes from "./src/routes/post.routes.mjs";
import getRoutes from "./src/routes/get.routes.mjs";
import deleteRoutes from "./src/routes/delete.routes.mjs";
import updateRoutes from "./src/routes/update.routes.mjs";
import specificRoutes from "./src/routes/specific.route.mjs";
import authRoutes from "./src/routes/auth/login.routes.mjs";
import chalk from "chalk";
// app routes
app.use(json());
app.use(cors());
app.use(express.urlencoded({ extended: true }));
app.use("/image", express.static("./uploads"));
app.use("/post", postRoutes);
app.use("/get", getRoutes);
app.use("/delete", deleteRoutes);
app.use("/update", updateRoutes);
app.use("/specific",specificRoutes);
app.use("/post",authRoutes);
// db connection
const dbOptions = {
useNewUrlParser: true,
useUnifiedTopology: true,
};
// connecting to the database
set("strictQuery", false);
connect(process.env.DB_URL, dbOptions)
.then(() => {
console.log(chalk.green("Connected to database successfully"));
})
.catch((err) => {
console.error(chalk.red("Connection error"), err);
process.exit(0);
});
// -------------- end of db connection ---------------------
app.listen(process.env.PORT, () => {
console.log(chalk.blueBright(`\nServer running on port http://${process.env.HOST_URL}:${process.env.PORT}\n`));
console.log(chalk.yellow('Waiting for connection from database.'));
});