-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
48 lines (38 loc) · 1.31 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
const express = require("express");
const bodyParser = require("body-parser");
const logger = require("morgan");
const mongoose = require("mongoose");
// const db = require("./models");
const PORT = process.env.PORT || 3001;
const app = express();
// Use morgan logger for logging requests
app.use(logger("dev"));
// Use body-parser for handling form submissions
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// Use express.static to serve the public folder as a static directory
if (process.env.NODE_ENV === "production") {
app.use(express.static("nyt-search-react/build"));
}
else {
app.use(express.static("public"));
}
// require('./routes')(app);
const api_routes = require('./routes/api');
app.use(api_routes);
app.get("*", function(req, res) {
res.sendFile(path.join(__dirname, "./nyt-search-react/public/index.html"));
});
// Connect to the Mongo DB
// mongoose.connect("mongodb://localhost:27017/scrapeAndComment");
const MONGODB_URI = process.env.MONGODB_URI || "mongodb://localhost:27017/nytreact";
// Set mongoose to leverage built in JavaScript ES6 Promises
// Connect to the Mongo DB
mongoose.Promise = Promise;
mongoose.connect(MONGODB_URI, {
useNewUrlParser: true
});
// Start the server
app.listen(PORT, function() {
console.log(`App running on port ${PORT}!`);
});