Skip to content

Commit

Permalink
add config.js to allow for changing api url depending on environment
Browse files Browse the repository at this point in the history
  • Loading branch information
ivan-unfolds committed May 2, 2024
1 parent dae4bd4 commit 30a3063
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/App.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useEffect, useState } from "react";
import config from "./config";
import reactLogo from "./assets/react.svg";
import viteLogo from "/vite.svg";
import "./App.css";
Expand All @@ -8,7 +9,7 @@ function App() {
const [questions, setQuestions] = useState([]);

useEffect(() => {
fetch("http://localhost:3000/api/questions/random")
fetch(`${config.apiBaseUrl}/api/questions/random`)
.then((res) => res.json())
.then(
(data) => {
Expand Down
13 changes: 13 additions & 0 deletions src/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
let config;

if (window.location.hostname === "localhost") {
config = {
apiBaseUrl: "http://localhost:3000",
};
} else {
config = {
apiBaseUrl: "https://api.example.com",
};
}

export default config;

0 comments on commit 30a3063

Please sign in to comment.