I have a React & Node & Express with was running locally and i want it to be expose to the internet. using this command
npm run dev — –host
I face this console log error :
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:8081/users. (Reason: CORS request did not succeed). Status code: (null).
I try to put this line in my code
vite.config :
import path from "path"
import react from "@vitejs/plugin-react"
import { defineConfig } from "vite"
export default defineConfig({
plugins: [react()],
resolve: {
alias: {
"@": path.resolve(__dirname, "./src"),
},
},
server: {
host: '0.0.0.0',
cors: true
}
})
server.js
app.use(cors({
origin: ['<http://http://0.0.0.0:5173/>', '<http://192.168.83.128:5173>'],
methods: ['GET', 'POST', 'PUT', 'DELETE', 'PATCH'],
allowedHeaders: ['Content-Type', 'Authorization']
}));