-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
32 lines (27 loc) · 916 Bytes
/
server.js
File metadata and controls
32 lines (27 loc) · 916 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
import express from "express";
import mongoose from "mongoose";
import userRouter from "./routes/user-routes.js";
import blogRouter from "./routes/blog-routes.js";
import cors from 'cors';
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/blog", blogRouter);
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'));
});
}
const PORT = process.env.PORT||5005;
app.listen(PORT,()=>console.log(`server listening on port ${PORT}`));