switch in .env for dev/prod

This commit is contained in:
fred 2025-07-25 16:00:55 -07:00
parent 773b4773eb
commit a3b0fffe45
7 changed files with 29 additions and 22 deletions

View file

@ -10,4 +10,5 @@ COPY . .
EXPOSE 3000 EXPOSE 3000
CMD ["npm", "run", "production"] # CMD ["npm", "run", "dev"]
CMD npm run $NODE_ENV

View file

@ -4,12 +4,7 @@ const port = 3000;
const cors = require("cors"); const cors = require("cors");
const app = express(); const app = express();
app.use( app.use(cors());
cors({
origin: "https://ec683cee72d30c5030.fredzernia.com/",
credentials: true,
}),
);
app.use(express.json()); app.use(express.json());
// ####### ROUTES ####### // ####### ROUTES #######

View file

@ -1,7 +1,7 @@
const knex = require('knex'); const knex = require('knex');
const knexConfig = require('./knexfile.js'); const knexConfig = require('./knexfile.js');
const environment = process.env.NODE_ENV || 'development'; const environment = process.env.NODE_ENV || 'dev';
const config = knexConfig[environment]; const config = knexConfig[environment];
const db = knex(config); const db = knex(config);

View file

@ -1,11 +1,11 @@
require("dotenv").config(); require("dotenv").config();
module.exports = { module.exports = {
development: { dev: {
client: "postgresql", client: "postgresql",
connection: { connection: {
host: "db", host: "db",
port: process.env.DB_PORT, port: 5432,
database: process.env.DB_NAME, database: process.env.DB_NAME,
user: process.env.DB_USER, user: process.env.DB_USER,
password: process.env.DB_PASSWORD, password: process.env.DB_PASSWORD,

View file

@ -16,23 +16,29 @@ services:
backend: backend:
image: recipes_backend image: recipes_backend
container_name: recipes_backend container_name: recipes_backend
build: ./backend build:
context: ./backend
args:
NODE_ENV: ${NODE_ENV}
ports: ports:
- "3000:3000" - "3000:3000"
volumes: volumes:
- ./backend:/usr/src/app - ./backend:/usr/src/app
environment: environment:
- NODE_ENV=production - NODE_ENV=${NODE_ENV}
- DB_USER=${DB_USER} - DB_USER=${DB_USER}
- DB_PASSWORD=${DB_PASSWORD} - DB_PASSWORD=${DB_PASSWORD}
- DB_NAME=${DB_NAME} - DB_NAME=${DB_NAME}
frontend: frontend:
image: recipes_frontend image: recipes_frontend
container_name: recipes_frontend container_name: recipes_frontend
build: ./frontend build:
context: ./backend
args:
NODE_ENV: ${NODE_ENV}
ports: ports:
- "8081:80" - "8081:80"
volumes: volumes:
- ./frontend:/usr/src/app - ./frontend:/usr/src/app
environment: environment:
- NODE_ENV=production - NODE_ENV=${NODE_ENV}

View file

@ -10,4 +10,5 @@ COPY . .
EXPOSE 80 EXPOSE 80
CMD ["npm", "run", "production"] # CMD ["npm", "run", "$NODE_ENV"]
CMD npm run $NODE_ENV

View file

@ -1,23 +1,27 @@
const baseUrl = process.env.NODE_ENV === 'production'
? '/'
: 'http://localhost:3000/';
export const getRecipes = async () => { export const getRecipes = async () => {
const response = await fetch("/backend/recipes"); const response = await fetch(`${baseUrl}backend/recipes`);
const data = await response.json(); const data = await response.json();
return data; return data;
}; };
export const getRecipeSteps = async () => { export const getRecipeSteps = async () => {
const response = await fetch("/backend/recipe-steps"); const response = await fetch(`${baseUrl}backend/recipe-steps`);
const data = await response.json(); const data = await response.json();
return data; return data;
}; };
export const getRecipeIngredients = async () => { export const getRecipeIngredients = async () => {
const response = await fetch("/backend/recipe-ingredients"); const response = await fetch(`${baseUrl}backend/recipe-ingredients`);
const data = await response.json(); const data = await response.json();
return data; return data;
}; };
export const getRecipeById = async (id) => { export const getRecipeById = async (id) => {
const response = await fetch(`/backend/recipe/${id}`); const response = await fetch(`${baseUrl}backend/recipe/${id}`);
const data = await response.json(); const data = await response.json();
return data; return data;
}; };
@ -25,7 +29,7 @@ export const getRecipeById = async (id) => {
export const addRecipe = async (recipeData) => { export const addRecipe = async (recipeData) => {
console.log(JSON.stringify(recipeData)); console.log(JSON.stringify(recipeData));
// return // return
const response = await fetch("/backend/add-recipe", { const response = await fetch(`${baseUrl}backend/add-recipe`, {
method: "POST", method: "POST",
headers: { "Content-Type": "application/json" }, headers: { "Content-Type": "application/json" },
body: JSON.stringify(recipeData), body: JSON.stringify(recipeData),
@ -38,7 +42,7 @@ export const addRecipe = async (recipeData) => {
export const setDBStars = async (id, stars) => { export const setDBStars = async (id, stars) => {
console.log(JSON.stringify({ id: id, stars: stars })); console.log(JSON.stringify({ id: id, stars: stars }));
// return // return
const response = await fetch("/backend/set-stars", { const response = await fetch(`${baseUrl}backend/set-stars`, {
method: "POST", method: "POST",
headers: { "Content-Type": "application/json" }, headers: { "Content-Type": "application/json" },
body: JSON.stringify({ id: id, stars: stars }), body: JSON.stringify({ id: id, stars: stars }),
@ -51,7 +55,7 @@ export const setDBStars = async (id, stars) => {
export const deleteRecipe = async (id) => { export const deleteRecipe = async (id) => {
console.log(id); console.log(id);
// return // return
const response = await fetch("/backend/delete-recipe", { const response = await fetch(`${baseUrl}backend/delete-recipe`, {
method: "DELETE", method: "DELETE",
headers: { "Content-Type": "application/json" }, headers: { "Content-Type": "application/json" },
body: JSON.stringify({ id }), body: JSON.stringify({ id }),