forked from fransiscushermanto/Chatter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
57 lines (51 loc) · 1.51 KB
/
app.js
File metadata and controls
57 lines (51 loc) · 1.51 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
require("dotenv").config();
const express = require("express");
const bodyParser = require("body-parser");
const favicon = require("express-favicon");
const path = require("path");
const cors = require("cors");
const morgan = require("morgan");
const mongoose = require("mongoose");
const connectionString = "mongodb://localhost/chatter";
if (process.env.NODE_ENV !== "test") {
mongoose.connect(process.env.MONGODB_URI, {
useNewUrlParser: true,
useUnifiedTopology: true
});
mongoose.connection.on("error", function(error) {
console.log(error);
console.log("ERROR CONNECT");
});
} else {
mongoose.connect(connectionString, {
useNewUrlParser: true,
useUnifiedTopology: true
});
}
const app = express();
//Middlewares
app.use(morgan("dev"));
app.use(cors());
app.use(bodyParser.json());
app.use(favicon(path.join(__dirname, "client", "build", "favicon.ico")));
app.use(express.static(__dirname));
app.use(express.static(path.join(__dirname, "client", "build")));
if (process.env.NODE_ENV !== "test") {
app.get("*", function(req, res) {
const index = path.join(__dirname, "client", "build", "index.html");
res.sendFile(index);
});
}
app.use(function(req, res, next) {
if (
process.env.NODE_ENV !== "test" &&
req.headers["x-forwarded-proto"] !== "https"
) {
return res.redirect("https://" + req.headers.host + req.url);
}
return next();
});
//Routes
app.use("/users", require("./server/routes/users"));
app.use("/chats", require("./server/routes/chats"));
module.exports = app;