-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
70 lines (55 loc) · 2.09 KB
/
server.js
File metadata and controls
70 lines (55 loc) · 2.09 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//---------------------CONTAINS ALL PACKAGES REQD BY APPN----------------------------------
const express = require("express");
const app = express();
const path = require("path");
const mongoose = require("mongoose");
const methodOverride = require("method-override");
const postsRouter = require("./routes/postRoutes");
const usersRouter = require("./routes/userRoutes");
const dotenv = require("dotenv");
const cors = require("cors");
const bodyParser = require("body-parser");
//----------------------FOR EJS FILES------------------------------------------------------
app.set("views", path.join(__dirname, "views"));
app.set("view engine", "ejs");
//----------------------FOR USING JSON AND FORM DATA ---------------------------------
app.use(express.urlencoded({ extended: true }));
app.use(bodyParser.json({ limit: "50mb" }));
app.use(bodyParser.urlencoded({ limit: "50mb", extended: true }));
app.use(cors());
dotenv.config();
app.use(express.json());
app.use(methodOverride("_method"));
//----------------------FOR ROUTES---------------------------------------------------------
app.use("/", postsRouter);
app.use("/", usersRouter);
//--------------------SERVE STATIC ASSETS IF IN PRODUCTION---------------------------------
if (process.env.NODE_ENV === "production") {
// set a static folder
app.use(express.static("client/build"));
// Provide a wildcard as a fallback for all routes
app.get("*", (req, res) => {
res.sendFile(path.resolve(__dirname, "client", "build", "index.html"));
});
}
//---------------------SETTING UP CONNECTION-----------------------------------------------
//config PORT
const PORT = process.env.PORT || 4000;
//config MongoDB
//const MONGO_URI = process.env.MONGO_URI;
//const MONGO_DB_NAME = process.env.MONGO_DB_NAME;
mongoose
.connect(process.env.MONGO_URL, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => {
console.log("MONGO CONNECTION OPEN!!!");
})
.catch((err) => {
console.log("OH NO MONGO CONNECTION ERROR!!!!");
console.log(err);
});
app.listen(PORT, () => {
console.log(`APP IS LISTENING ON PORT ${PORT}`);
});