-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.js
More file actions
41 lines (29 loc) · 1008 Bytes
/
Server.js
File metadata and controls
41 lines (29 loc) · 1008 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
import express from "express";
import mongoose from "mongoose";
import userrouter from "./routes/User-routes.js";
import cors from "cors";
import courseRouter from "./routes/Course-routes.js";
import dotenv from 'dotenv';
import path from 'path';
dotenv.config();
const App = express();
mongoose.connect(process.env.CONNECTION_URL)
.then(()=>console.log("Connected to database"))
.catch((err) => console.log(err))
App.use(cors());
App.use(express.json());
App.use("/api/user",userrouter);
App.use("/api/course",courseRouter)
// ------------deployment------------
const __dirname = path.resolve();
//Serve Static assets if in production
if(process.env.NODE_ENV === 'production')
{
App.use(express.static('client/build'));
App.get('*',(req,res)=>{
res.sendFile(path.resolve(__dirname,'client','build','index.html'));
});
}
// ------------deployment--------------
const PORT = process.env.PORT;
App.listen(PORT||5000,()=>console.log(`server listening on port ${PORT}`));