-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
50 lines (44 loc) · 1.26 KB
/
index.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
import express from "express";
import mongoose from "mongoose";
import dotenv from "dotenv";
import userRouter from "./routes/user.route.js";
import cookieParser from "cookie-parser";
import authRouter from "./routes/auth.route.js";
import listingRouter from "./routes/listing.route.js";
import path from 'path'
dotenv.config();
const __dirname = path.resolve()
//initializing express app
const app = express();
app.use(express.json());
app.use(cookieParser())
app.listen(3000, () => {
console.log(`Server is running on port 3000`);
});
//connecting to MONGODB
mongoose
.connect(process.env.MONGO)
.then(() => {
console.log("connected to MongoDB");
})
.catch((err) => {
console.log(err);
});
//Routes
app.use("/api/user", userRouter);
app.use("/api/auth", authRouter);
app.use("/api/listing", listingRouter);
app.use(express.static(path.join(__dirname, '/client/dist')))
app.get('*', (req, res)=>{
res.sendFile(path.join(__dirname, 'client', 'dist', 'index.html'))
})
// creating a middleware to handle error || success msg
app.use((err, req, res, next) => {
const statusCode = err.statusCode || 500;
const message = err.message || "Internal Server Error";
return res.status(statusCode).json({
success: false,
statusCode,
message,
});
});