Skip to content

Instantly share code, notes, and snippets.

@sp0033212000
Last active July 15, 2020 15:10
Show Gist options
  • Save sp0033212000/f85b312b578d4a9f3bfc88b52d66e529 to your computer and use it in GitHub Desktop.
Save sp0033212000/f85b312b578d4a9f3bfc88b52d66e529 to your computer and use it in GitHub Desktop.
// dev.js - don't commit this!!!
type Config = { mongoURI: string; cookieKey: string };
const config: Config = {
mongoURI:
"mogoUrl",
cookieKey: "cookieKey",
};
export { config };
type Config = { mongoURI: string; cookieKey: string };
//key.js - figure out what set of credentials to return
const keys = async () => {
let resConfig: Config;
if (process.env.NODE_ENV === "production") {
//we are in production - return the prod set of keys
const { config } = await import("./dev");
resConfig = config;
} else {
//we are in development - return the dev keys!!!
const { config } = await import("./dev");
resConfig = config;
}
return resConfig;
};
export { keys };
//prod.js - production keys here!!
type Config = { mongoURI: string; cookieKey: string };
const config: Config = {
mongoURI: process.env.MONGO_URI,
cookieKey: process.env.COOKIE_KEY,
};
export { config };
import express from "express";
import mongoose from "mongoose";
import cookieSession from "cookie-session";
import { json } from "body-parser";
import { keys } from "./config/keys";
require("./models/User");
require("./models/Survey");
require("./services/passport");
mongoose.connect(keys.mongoURI);
const app = express();
app.use(json());
app.use(
cookieSession({
maxAge: 30 * 24 * 60 * 60 * 1000,
keys: [keys.cookieKey],
})
);
require("./routes/authRoutes")(app);
require("./routes/billingRoutes")(app);
require("./routes/surveyRoutes")(app);
if (process.env.NODE_ENV === "production") {
//Express will serve up production assets
//like our main.js file, or main.css file!
app.use(express.static("client/build"));
//Express will serve up the index.html file
//if it doesn't recognize the route
const path = require("path");
app.get("*", (req, res) => {
res.sendFile(path.resolve(__dirname, "client", "build", "index.html"));
});
}
const PORT = process.env.PORT || 5000;
app.listen(PORT);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment