-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
40 lines (35 loc) · 1.19 KB
/
app.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
require('dotenv').config()
const express = require("express");
const morgan = require("morgan");
const mongoose = require("mongoose");
const cors = require("cors");
const cookieParser = require('cookie-parser');
const app = express();
// middleware
if (process.env.NODE_ENV === "development") {
app.use(cors({ origin: "http://localhost:8081", credentials: true }));
} else {
app.use(cors());
}
app.use(morgan("dev"));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(cookieParser());
// connect db
mongoose.connect(process.env.DB_URI, { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => console.log("Server has cocking!!!"))
.catch((err) => console.log(`ERROR - MongoDB not connected : ${err} `));
// register api routes
const apiRoutes = require("./routes");
const path = require("path");
app.use("/api", apiRoutes)
// register SPA routes
app.use(express.static(path.join(__dirname + "/public")));
app.get("*", (req, res) => {
res.sendFile(path.resolve(__dirname, "frontend/public", "index.html"));
});
// run server
const port = process.env.PORT || 3000
app.listen(port, () => {
console.log(`has been chaos on http://localhost:${port}`)
});