Pointing the Frontend at Railway
Your React app currently points at http://localhost:3000. In production it needs to point at your Railway API. The standard approach is an environment variable that Vite swaps at build time.
Vite environment variables
Section titled “Vite environment variables”Vite exposes environment variables to your app if their name starts with VITE_:
# .env.development (used by npm run dev)VITE_API_URL=http://localhost:3000
# .env.production (used by npm run build)VITE_API_URL=https://your-app.railway.appRead the variable in your API client:
import axios from 'axios'
const apiClient = axios.create({ baseURL: import.meta.env.VITE_API_URL,})
export default apiClientimport.meta.env.VITE_API_URL is replaced with the actual string at build time — not at runtime. When you run npm run build, Vite bundles the production value directly into the output files.
Finding your Railway URL
Section titled “Finding your Railway URL”In the Railway dashboard, open your backend service. The URL is shown under Settings → Networking → Public Domain. It looks like https://bulletin-api-production-xxxx.up.railway.app.
Committing .env files safely
Section titled “Committing .env files safely”.env.local.env.*.localVite’s .env.development and .env.production files don’t contain secrets (only a public URL), so they can be committed. Never commit .env.local or any file containing API keys, database credentials, or JWT secrets.
CORS on the deployed backend
Section titled “CORS on the deployed backend”When the frontend is deployed to GitHub Pages and the backend is on Railway, they’re on different domains — CORS applies. Check your Express CORS config:
// src/index.ts (backend) — update if neededapp.use(cors({ origin: [ 'http://localhost:5173', // Vite dev server 'https://your-github-username.github.io', // GitHub Pages ],}))Add the GitHub Pages origin to the allowed list. Redeploy to Railway after the change.
Testing the production config locally
Section titled “Testing the production config locally”You can run Vite in production mode without deploying:
npm run build && npm run previewnpm run preview serves the built output locally on port 4173. It uses .env.production values — a quick way to confirm the production URL is wired correctly before you push.
Exercise
Section titled “Exercise”Open src/api/apiClient.ts in your Bulletin frontend project.
- Create
.env.developmentwithVITE_API_URL=http://localhost:3000. - Create
.env.productionwithVITE_API_URL=https://your-app.railway.app(use your actual URL). - Update
apiClient.tsto useimport.meta.env.VITE_API_URLas thebaseURL. - Run
npm run devand confirm the app still works against local backend. - Update your backend CORS config to include your GitHub Pages domain and push it to Railway.
VITE_prefix exposes env vars to the browser bundle viaimport.meta.env.VITE_*..env.developmentis used bynpm run dev;.env.productionis used bynpm run build.- Values are baked in at build time — not read at runtime.
- Update backend CORS to include the GitHub Pages domain before deploying.