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
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
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β¦Okay will create one in a few min
I looked at your code a little closer. πΈ
You're importing an
express server into a React component, right?Yes
What do you exect to happen? You're familiar with the "Client" (browser) / "Server" (internet) model?
express is for building servers.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?
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.
It wouldn't hurt.
GitHub
GitHub - Anes039/Dog-Facts
Contribute to Anes039/Dog-Facts development by creating an account on GitHub.
Sorry for late response
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.and then the issue will be fixed?
It's a start. Your code is still pretty rough, but it's a start when combined with the
fetch documentation I linked earlier.okay