Cors Error

const [dogData,setDogData] = useState([]); const express = require('express'); const app = express(); const cors = require('cors'); app.cors({ origin:'*' }) useEffect(() => { const fetchDogData = async () =>{ try { const response = await fetch ('https://www.billplz-sandbox.com/api', ); const data = await response.json(); console.log(data); setDogData(data); } catch (err){ console.log(err); } } fetchDogData(); } , []) return ( <div></div> ) } I am trying to fetch data from that api also i used this method but after that i got this message in my screen ..... Module not found: Error: Can't resolve 'zlib' in 'C:\Users\HP\Desktop\React\my-app\node_modules\body-parser\lib' BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default. This is no longer the case. Verify if you need this module and configure a polyfill for it. If you want to include a polyfill, you need to: - add a fallback 'resolve.fallback: { "zlib": require.resolve("browserify-zlib") }' - install 'browserify-zlib' If you don't want to include a polyfill, you can use an empty module like this: resolve.fallback: { "zlib": false } Without using cors i got a Cors error something related to header
15 Replies
Jochem
Jochemβ€’11mo ago
please just be patient, it might be a while before someone gets around to answering. And don't try to @ everyone, it's rude but also disabled and will time you out automatically
dys πŸ™
dys πŸ™β€’11mo ago
Do you know how to take a screen shot? On Linux, if you hit Shift+PrintScreen it'll give you crosshairs & let you pick what portion of the screen you want to capture. Do you know what system is being used to bootstrap your app? It says webpack, so Create React App? Or is it a Next.js app? body-parser is generally used by an express server to parse the request body. It shouldn't be being bundled for use on the frontend… :confused_dog: It's hard to diagnose without more specifics. A GitHub repository is optimal…
anes039
anes039OPβ€’11mo ago
Okay will create one in a few min
dys πŸ™
dys πŸ™β€’11mo ago
I looked at your code a little closer. 😸 You're importing an express server into a React component, right?
anes039
anes039OPβ€’11mo ago
Yes
dys πŸ™
dys πŸ™β€’11mo ago
What do you exect to happen? You're familiar with the "Client" (browser) / "Server" (internet) model? express is for building servers.
anes039
anes039OPβ€’11mo ago
Okay then show me a better solution because i want to get that data i just sent request , to get it in console Should i create a repo?
dys πŸ™
dys πŸ™β€’11mo ago
MDN Web Docs
Using the Fetch API - Web APIs | MDN
The Fetch API provides a JavaScript interface for accessing and manipulating parts of the protocol, such as requests and responses. It also provides a global fetch() method that provides an easy, logical way to fetch resources asynchronously across the network.
dys πŸ™
dys πŸ™β€’11mo ago
It wouldn't hurt.
anes039
anes039OPβ€’11mo ago
GitHub
GitHub - Anes039/Dog-Facts
Contribute to Anes039/Dog-Facts development by creating an account on GitHub.
anes039
anes039OPβ€’11mo ago
Sorry for late response
dys πŸ™
dys πŸ™β€’11mo ago
create-react-app was last updated 3–4 years ago. Try creating an app with: npm create vite@latest my-app -- --template react-ts That will almost certainly run better.
anes039
anes039OPβ€’11mo ago
and then the issue will be fixed?
dys πŸ™
dys πŸ™β€’11mo ago
It's a start. Your code is still pretty rough, but it's a start when combined with the fetch documentation I linked earlier.
anes039
anes039OPβ€’11mo ago
okay
Want results from more Discord servers?
Add your server