Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions backend/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ const initSocketServer = require('./src/sockets/socket.server')

connectDB()
initSocketServer(httpServer)
httpServer.listen(3000,()=>{
console.log('server is running at http://localhost:3000/')
const PORT = process.env.PORT || 3001
httpServer.listen(PORT,()=>{
console.log(`server is running at http://localhost:${PORT}/`)
})

20 changes: 19 additions & 1 deletion backend/src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,25 @@ const cookieParser = require('cookie-parser')

// cors
app.use(cors({
origin: ['https://contextual-chatbot-react.vercel.app', 'http://localhost:5173', 'https://contextual-chatbot-react.onrender.com'],
origin: function (origin, callback) {
// Allow requests with no origin (like mobile apps or curl requests)
if (!origin) return callback(null, true);

const allowedOrigins = [
'https://contextual-chatbot-react.vercel.app',
'http://localhost:5173',
'http://localhost:3000',
'http://localhost:3001',
'https://contextual-chatbot-react.onrender.com'
];

if (allowedOrigins.includes(origin)) {
callback(null, true);
} else {
console.log('CORS blocked origin:', origin);
callback(new Error('Not allowed by CORS'));
}
},
methods: ['GET', 'HEAD', 'PUT', 'PATCH', 'POST', 'DELETE', 'OPTIONS'],
allowedHeaders: ['Content-Type', 'Authorization', 'X-Requested-With', 'Accept', 'Origin'],
exposedHeaders: ['Set-Cookie'],
Expand Down
21 changes: 20 additions & 1 deletion backend/src/sockets/socket.server.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,27 @@ const { createMemory, queryMemory } = require("../services/vector.service");
const initSocketServer = (httpServer) => {
const io = new Server(httpServer, {
cors: {
origin: process.env.CORS_ORIGIN || "http://localhost:5173",
origin: function (origin, callback) {
if (!origin) return callback(null, true);

const allowedOrigins = [
'https://contextual-chatbot-react.vercel.app',
'http://localhost:5173',
'http://localhost:3000',
'http://localhost:3001',
'https://contextual-chatbot-react.onrender.com'
];

if (allowedOrigins.includes(origin)) {
callback(null, true);
} else {
console.log('Socket CORS blocked origin:', origin);
callback(new Error('Not allowed by CORS'));
}
},
credentials: true,
methods: ['GET', 'POST', 'OPTIONS'],
allowedHeaders: ['Content-Type', 'Authorization', 'X-Requested-With', 'Accept', 'Origin']
},
});

Expand Down
4 changes: 2 additions & 2 deletions frontend/src/api/axios.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { logout } from '../redux/reducers/authSlice';
// import axios from "axios";

export const axiosInstance = axios.create({
// baseURL: "http://localhost:3000/api",
baseURL: "https://contextual-chatbot-react.onrender.com/api",
baseURL: "http://localhost:3001/api",
// baseURL: "https://contextual-chatbot-react.onrender.com/api",
withCredentials: true,
});
// intercepters
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/ChatInterface.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ const ChatInterface = () => {

useEffect(() => {
dispatch(getChats()); // This fetches chats and then all messages
const newSocket = io("https://contextual-chatbot-react.onrender.com", { withCredentials: true });
const newSocket = io("http://localhost:3001", { withCredentials: true });
setSocket(newSocket);
return () => newSocket.disconnect();
}, [dispatch]);
Expand Down