-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
50 lines (37 loc) · 1014 Bytes
/
server.js
File metadata and controls
50 lines (37 loc) · 1014 Bytes
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 cors from 'cors';
import path from 'path';
import bookRoute from './route/book.route.js'
import userRoute from './route/user.route.js'
const app = express();
app.use(cors());
app.use(express.json())
dotenv.config();
const PORT = process.env.PORT || 5000;
const URI = process.env.MongoDBURI;
//connect to mongoDB
try{
mongoose.connect(URI,{
// useNewUrlParser : true,
// useUnifiedTopology : true,
});
console.log("connected to mongoDB");
}catch(err){
console.log("Error: ",err);
}
//defining routes
app.use("/book",bookRoute)
app.use("/user",userRoute)
// deplloyment
if(process.env.NODE_ENV ==="production"){
const dirPath = path.resolve();
app.use(express.static('frontend/dist'));
app.get("*",(req,res) =>{
res.sendFile(path.resolve(dirPath,"frontend","dist","index.html"));
} )
}
app.listen(PORT,()=>{
console.log(`Server is listening on the port ${PORT}`);
});