-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathserver.js
More file actions
64 lines (56 loc) · 2 KB
/
server.js
File metadata and controls
64 lines (56 loc) · 2 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
58
59
60
61
62
63
64
const express = require("express");
const PORT = process.env.PORT || 3001;
const app = express();
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
const CryptoJS = require('crypto-js');
const dotenv = require('dotenv');
const routes = require("./routes");
dotenv.config();
// Configure body parser for AJAX requests
app.use(bodyParser.urlencoded({ limit: "20mb", extended: false }));
app.use(bodyParser.json({ limit: "20mb" }));
// Serve up static assets (usually on heroku)
if (process.env.NODE_ENV === "production") {
app.use(express.static("client/build"));
} else {
app.use(express.static("client/src"));
}
// handles admin password
app.get("/login/:password", function(req, res) {
const hash = CryptoJS.HmacSHA256(req.params.password, process.env.PSWD_SECRET || 'local_test');
if (process.env.PSWD_SECRET) {
if (decodeURI(hash) === "2ceff18447880e20f63796ae330ca5ef90de5fdc576068ee3422a8bb7503225f") {
res.status(200).send("Authorized");
} else {
res.status(401).send("Unauthorized");
};
} else {
if (decodeURI(hash) === "065730ada2dd4669658f3b6221dd543d8073708a0d00309d7bff2d8b57c538cc") {
res.status(200).send("Authorized");
} else {
res.status(401).send("Unauthorized");
};
}
});
// Add routes, both API and view
app.use(routes);
// Set up promises with mongoose
mongoose.Promise = global.Promise;
let mongoURL = "mongodb://localhost/tromboneCollection";
if (process.env.NODE_ENV === 'production') {
mongoURL = `mongodb+srv://trombone_admin:${process.env.MONGOPSWD}@trombonecollection.fsizb.mongodb.net/${process.env.MONGODBNAME}?retryWrites=true&w=majority`;
}
// Connect to the Mongo DB
mongoose.connect(
mongoURL,
{ useNewUrlParser: true }
);
// 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"));
});
app.listen(PORT, function() {
console.log(`🌎 ==> Server now on port ${PORT}!`);
});