-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
49 lines (40 loc) · 1.29 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
const express = require("express");
const path = require("path");
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
const apiRoutes = require("./routes/apiRoutes");
const app = express();
const PORT = process.env.PORT || 3001;
// Serve up static assets (usually on heroku)
// Needed because we use hot loading during development
if (process.env.NODE_ENV === "production") {
app.use(express.static("client/build"));
}
// Configure body parser for AJAX requests
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
// Add routes, both API and view
app.use("/api", apiRoutes);
// 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"));
});
// Set up promises with mongoose
mongoose.Promise = global.Promise;
// Connect to the Mongo DB
mongoose.connect(
process.env.MONGODB_URI || "mongodb://localhost/nytreact",
{
useMongoClient: true
}
);
mongoose.connection.on("error", function(error) {
console.log("Mongoose Error: ", error);
});
mongoose.connection.once("open", function(){
console.log("Mongoose connection successful.");
});
app.listen(PORT, function() {
console.log(`🌎 ==> Server now on port ${PORT}!`);
});