104 lines
2.8 KiB
JavaScript
104 lines
2.8 KiB
JavaScript
const express = require("express");
|
|
const cors = require("cors");
|
|
const bodyParser = require("body-parser");
|
|
const path = require("path");
|
|
const compression = require("compression");
|
|
const twilio = require("twilio");
|
|
//var enforce = require("express-sslify");
|
|
|
|
require("dotenv").config({
|
|
path: path.resolve(
|
|
process.cwd(),
|
|
`.env.${process.env.NODE_ENV || "development"}`
|
|
),
|
|
});
|
|
|
|
const https = require("https");
|
|
const fs = require("fs");
|
|
|
|
const app = express();
|
|
const port = process.env.PORT || 5000;
|
|
//const port = 5000;
|
|
|
|
app.use(compression());
|
|
app.use(bodyParser.json());
|
|
app.use(bodyParser.urlencoded({ extended: true }));
|
|
//app.use(enforce.HTTPS({ trustProtoHeader: true }));
|
|
app.use(cors());
|
|
|
|
//Email Based Paths.
|
|
var sendEmail = require("./sendemail.js");
|
|
app.post("/sendemail", sendEmail.sendEmail);
|
|
|
|
//Test route to ensure Express is responding.
|
|
app.get("/test", function (req, res) {
|
|
res.status(200).send();
|
|
});
|
|
|
|
//Cloudinary Media Paths
|
|
var media = require("./server/media/media");
|
|
app.post("/media/sign", media.createSignedUploadURL);
|
|
app.post("/media/download", media.downloadFiles);
|
|
|
|
//SMS/Twilio Paths
|
|
var smsReceive = require("./server/sms/receive");
|
|
app.post(
|
|
"/sms/receive",
|
|
twilio.webhook({ validate: process.env.NODE_ENV === "PRODUCTION" }),
|
|
smsReceive.receive
|
|
);
|
|
var smsSend = require("./server/sms/send");
|
|
app.post("/sms/send", smsSend.send);
|
|
var smsStatus = require("./server/sms/status");
|
|
app.post(
|
|
"/sms/status",
|
|
twilio.webhook({ validate: process.env.NODE_ENV === "PRODUCTION" }),
|
|
smsStatus.status
|
|
);
|
|
|
|
//Handlebars Paths for Email/Report Rendering
|
|
var renderHandlebars = require("./server/render/renderHandlebars");
|
|
app.post("/render", renderHandlebars.render);
|
|
|
|
var fb = require("./server/firebase/firebase-handler");
|
|
app.post("/notifications/send", fb.sendNotification);
|
|
|
|
//Serve React App if in Production
|
|
if (process.env.NODE_ENV === "production") {
|
|
app.use(express.static(path.join(__dirname, "client/build")));
|
|
|
|
app.get("/service-worker.js", (req, res) => {
|
|
res.sendFile(path.resolve(__dirname, "..", "build", "service-worker.js"));
|
|
});
|
|
|
|
app.get("*", function (req, res) {
|
|
res.sendFile(path.join(__dirname, "client/build", "index.html"));
|
|
});
|
|
}
|
|
|
|
if (process.env.NODE_ENV === "production") {
|
|
app.listen(port, (error) => {
|
|
if (error) throw error;
|
|
console.log("[PRODUCTION] Server running on port " + port);
|
|
});
|
|
} else {
|
|
https
|
|
.createServer(
|
|
{
|
|
key: fs.readFileSync("./key.pem"),
|
|
cert: fs.readFileSync("./cert.pem"),
|
|
passphrase: "Wl0d8k@!",
|
|
},
|
|
app
|
|
)
|
|
.listen(port, (error) => {
|
|
if (error) throw error;
|
|
console.log("[DEV/STAGING] Mock HTTPS Server running on port " + port);
|
|
});
|
|
}
|
|
|
|
// app.listen(port, error => {
|
|
// if (error) throw error;
|
|
// console.log("Server running on port " + port);
|
|
// });
|