diff --git a/backend/.env b/backend/.env deleted file mode 100644 index 9624cc077..000000000 --- a/backend/.env +++ /dev/null @@ -1,3 +0,0 @@ -MONGODB_URI=mongodb+srv://binayakb1604:sfnkgYCZXLJ1lXSk@cluster0.wzmqjc0.mongodb.net/ -PORT=8080 -JWT_SECRET=RjFSmUXkC/N/icMei94QhPeJtV6voBu5fZQzrzKcEhY= diff --git a/frontend/.env b/frontend/.env new file mode 100644 index 000000000..677dcb560 --- /dev/null +++ b/frontend/.env @@ -0,0 +1 @@ +NEXT_PUBLIC_API_BASE_URL=https://stackit-etwq.onrender.com diff --git a/frontend/src/components/Home.jsx b/frontend/src/components/Home.jsx index a0230f793..a274ef6bd 100644 --- a/frontend/src/components/Home.jsx +++ b/frontend/src/components/Home.jsx @@ -2,6 +2,7 @@ import React, { useState, useEffect } from 'react'; import { Link, useSearchParams, useNavigate } from 'react-router-dom'; import axios from 'axios'; import './Questions.css'; +import api from '../index'; const Questions = () => { const [questions, setQuestions] = useState([]); @@ -51,7 +52,7 @@ const Questions = () => { url += `&search=${encodeURIComponent(search)}`; } - const res = await axios.get(url); + const res = await api.get(url); if (res.data.success) { setQuestions(res.data.questions); diff --git a/frontend/src/components/Profile.jsx b/frontend/src/components/Profile.jsx index 839549dbd..cc482022f 100644 --- a/frontend/src/components/Profile.jsx +++ b/frontend/src/components/Profile.jsx @@ -1,6 +1,7 @@ import React, { useState, useEffect } from 'react'; import { useNavigate } from 'react-router-dom'; import axios from 'axios'; +import api from '../index'; const Profile = () => { const navigate = useNavigate(); @@ -24,7 +25,7 @@ const Profile = () => { } }; - const res = await axios.get('/api/auth/me', config); + const res = await api.get('/auth/me', config); if (res.data.success) { setUser(res.data.user); diff --git a/frontend/src/components/QuestionDetail.jsx b/frontend/src/components/QuestionDetail.jsx index 1cead92ec..ddc0f9752 100644 --- a/frontend/src/components/QuestionDetail.jsx +++ b/frontend/src/components/QuestionDetail.jsx @@ -5,6 +5,7 @@ import ReactQuill from 'react-quill'; import 'react-quill/dist/quill.snow.css'; import { Paper, Card, Box, Typography, Button, Chip, Alert, Divider } from '@mui/material'; import { styled } from '@mui/material/styles'; +import api from '../index'; const QuestionDetail = () => { const { id } = useParams(); @@ -31,7 +32,7 @@ const QuestionDetail = () => { setLoading(true); setError(''); try { - const res = await axios.get(`/api/questions/${id}`); + const res = await api.get(`/questions/${id}`); if (res.data.success) { setQuestion(res.data.question); } else { @@ -63,11 +64,11 @@ const QuestionDetail = () => { } }; const body = JSON.stringify({ description: answerText }); - const res = await axios.post(`/api/answers/${id}`, body, config); + const res = await api.post(`/answers/${id}`, body, config); if (res.data.success) { setAnswerText(''); // Refresh question to show new answer - const updated = await axios.get(`/api/questions/${id}`); + const updated = await api.get(`/questions/${id}`); setQuestion(updated.data.question); } } catch (err) { @@ -92,7 +93,7 @@ const QuestionDetail = () => { } }; const body = { type }; - const res = await axios.post(`/api/answers/${answerId}/vote`, body, config); + const res = await api.post(`/answers/${answerId}/vote`, body, config); if (res.data.success !== false) { // Update the answer's vote count in local state instantly setQuestion(prev => { @@ -136,7 +137,7 @@ const QuestionDetail = () => { } }; const body = JSON.stringify({ text }); - const res = await axios.post(`/api/answers/${answerId}/comments`, body, config); + const res = await api.post(`/answers/${answerId}/comments`, body, config); if (res.data.success) { setQuestion(prev => { if (!prev) return prev; diff --git a/frontend/src/components/SignIn.js b/frontend/src/components/SignIn.js index 55ef48ef6..8447ac6d9 100644 --- a/frontend/src/components/SignIn.js +++ b/frontend/src/components/SignIn.js @@ -9,6 +9,7 @@ import { } from '@mui/material'; import { Link, useNavigate } from 'react-router-dom'; import axios from 'axios'; +import api from '../index'; const SignIn = () => { const navigate = useNavigate(); @@ -29,7 +30,7 @@ const SignIn = () => { try { const config = { headers: { 'Content-Type': 'application/json' } }; - const res = await axios.post('/api/auth/login', JSON.stringify(formData), config); + const res = await api.post('/auth/login', JSON.stringify(formData), config); if (res.data.success) { localStorage.setItem('token', res.data.token); diff --git a/frontend/src/index.js b/frontend/src/index.js index 7a40b297c..1ead2e773 100644 --- a/frontend/src/index.js +++ b/frontend/src/index.js @@ -1,6 +1,13 @@ import React from 'react'; import ReactDOM from 'react-dom/client'; import App from './App'; +import axios from 'axios'; + +const api = axios.create({ + baseURL: process.env.NEXT_PUBLIC_API_BASE_URL, +}); + +export default api; const root = ReactDOM.createRoot(document.getElementById('root')); root.render(