forked from meredithajones/Fridge_For_All
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
67 lines (54 loc) · 1.7 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
55
56
57
58
59
60
61
62
63
64
65
66
67
const express = require("express");
const mongoose = require("mongoose");
const path = require("path");
// const bodyParser = require("body-parser");
const logger = require("morgan");
const dotenv = require("dotenv")
const cookieParser = require("cookie-parser")
const cors = require("cors")
const routes= require("./routes")
dotenv.config()
const PORT = process.env.PORT || 3001;
const app = express();
//Middleware
app.use(logger("dev"));
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(cookieParser());
app.use(function(req, res, next) {
res.header('Access-Control-Allow-Origin', "*");
res.header('Access-Control-Allow-Headers', true);
res.header('Access-Control-Allow-Credentials', 'Content-Type');
res.header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
next();
});
app.use(cors({
Origin: [
"localhost:3000",
"https://fridge-for-all-philly.herokuapp.com/"
],
credentials: false
}));
// Serve up static assets (usually on heroku)
if (process.env.NODE_ENV === "production") {
app.use(express.static("client/build"));
}
mongoose.connect(process.env.MONGODB_URI || "mongodb://localhost/fridge_for_all", {
useNewUrlParser: true,
useFindAndModify: false,
useUnifiedTopology: true
});
app.use("/auth", require("./routes/api/userRouter"))
app.use(routes)
// Send every request to the React app
// Define any API routes before this runs
app.get("*", function(req, res) {
res.sendFile(path.join(__dirname, "./client/build/index.html"));
});
// db.on("error", error => {
// console.log("Database Error:", error);
// });
app.listen(PORT, function() {
console.log(`🌎 ==> API server now on port ${PORT}!`);
});
module.exports = app;