-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
54 lines (41 loc) · 1.3 KB
/
server.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
import express from 'express';
import cors from 'cors';
import connectDB from './config/db.js';
import UserRoute from './routes/api/users.js';
import PostRoute from './routes/api/posts.js';
import AuthRoute from './routes/api/auth.js';
import ProfileRoute from './routes/api/profile.js';
import path from 'path';
import {fileURLToPath} from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const app=express();
//Database Call
connectDB();
const port=process.env.PORT || 5500;
//similar to bodyparser to get req.body
app.use(express.json({extended:false}));
app.use(cors());
// app.get('/',(req,res)=>
// {
// res.send("API running");
// })
app.use('/api/users',UserRoute);
app.use('/api/posts',PostRoute);
app.use('/api/profile',ProfileRoute);
app.use('/api/auth',AuthRoute);
//serve static assets 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'));
})
}
//static
app.use(express.static(path.join(__dirname,'./client/build')));
app.get('*',function(req,res){
res.sendFile(path.join(__dirname,'./client/build/index.html'));
});
app.listen(port,()=>{
console.log(`App listening at port ${port}`);
})