-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
53 lines (40 loc) · 1.35 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
import router from "./router"
import express from "express"
import { DB_URL } from "./config"
import { errorHandler } from "./middlewares/errorHandler"
import mongoose from "mongoose"
import cors from "cors"
import bodyParser from 'body-parser';
import multer from "multer"
import path from "path"
const app = express()
// make monggose db connection
var upload = multer();
mongoose.connect(DB_URL, { useNewUrlParser: true, useUnifiedTopology: true });
const db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function () {
// we're connected!
console.log("connected to Facebook_clone Database")
});
global.appRoot = path.resolve(__dirname);
app.use(cors())
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use("/api", router)
app.use('/uploads', express.static('uploads'))
// ...
// Right before your app.listen(), add this:
if (process.env.NODE_ENV == "production") {
app.use(express.static(path.join(__dirname, "client/build")))
app.get("*", (req, res) => {
res.sendFile(path.resolve(__dirname, "client", "build", "index.html"));
});
} else {
app.get("/", (req, res) => {
res.send("API is running")
})
}
app.use(errorHandler)
const PORT = process.env.PORT || 8000
app.listen(PORT, () => console.log(`listening at port ${PORT}`))