-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
53 lines (48 loc) · 1.5 KB
/
index.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
// Import dependencies
//--------------------------------------------------------
require("dotenv").config();
const express = require("express");
const http = require("http");
const morgan = require("morgan");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const cors = require("cors");
const keys = require("./keys");
const path = require("path");
const app = express();
const server = http.createServer(app);
const routes = require("./routes");
const PORT = process.env.PORT;
// Configure Express server
//--------------------------------------------------------
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(morgan("combined"));
app.use(routes);
// Configure Mongoose
//--------------------------------------------------------
mongoose.Promise = global.Promise;
mongoose
.connect(keys.MONGODB_URI)
.then(() => {
console.log("connected to database");
})
.catch(err => console.log(err));
if (["prod", "ci"].includes(process.env.NODE_ENV)) {
// scope static assets to express
app.use(express.static(path.join(__dirname, "client/public")));
// send SPA files to client
app.use("*", (req, res, next) => {
res.send(
"*",
res.sendFile(path.join(__dirname, "client/public/index.html")),
);
});
}
// Start server
//--------------------------------------------------------
const expressServer = app.listen(PORT, () => {
console.log("listening to port: ", PORT);
});
module.exports = expressServer;