-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
56 lines (51 loc) · 1.67 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
45
46
47
48
49
50
51
52
53
54
55
56
import Express from "express";
import AuthRoutes from "./routes/AuthRoutes.js";
import PostRoutes from "./routes/PostRoutes.js";
import UserRoutes from "./routes/UserRoutes.js";
import ChatRoutes from "./routes/ChatRoutes.js";
import NotificationRoutes from "./routes/NotificationRoutes.js";
import mongoose from "mongoose";
import cookieParser from "cookie-parser";
import path from "path"
import dotenv from "dotenv"
dotenv.config({ path: ".env" });
const app = Express();
app.use(Express.json());
app.use(cookieParser());
const connect = async () => {
await mongoose.connect(process.env.MONGO_URL,
{
useNewUrlParser: true, useUnifiedTopology: true,
});
console.log("connected to database");
}
connect();
app.use('/api/auth', AuthRoutes);
app.use('/api/post', PostRoutes);
app.use('/api/user', UserRoutes);
app.use('/api/chat', ChatRoutes);
app.use('/api/notification', NotificationRoutes);
app.use((err, req, res, next) => {
const errorStatus = err.status || 500;
const errorMessage = err.message || "Something went wrong!";
return res.status(errorStatus).json({
success: false,
status: errorStatus,
message: errorMessage,
stack: err.stack,
})
})
if (process.env.NODE_ENV === "production") {
app.use(Express.static(path.resolve(path.resolve(), 'client', 'build')));
app.get("*", (req, res) => {
res.sendFile(path.resolve(path.resolve(), 'client', 'build', 'index.html'), function (err) {
if (err) {
res.status(500).send(err)
}
});
})
}
const PORT = process.env.PORT || 8800;
app.listen(PORT, () => {
console.log(`connected on of ${PORT}`);
})