Closed fredericomattos closed 3 years ago
CORS is disabled by default on five-server.
To enable it add a config file:
// .fiveserverrc
{
"cors": true
}
But, it does not look like five-server is the issue here. The browser simply does not accept the request from livescore.com since it does not send the necessary headers.
To make it work you have to request the data from your own server.
See the example below:
<!DOCTYPE html>
<html lang="en">
<body>
<script>
const main = async () => {
try {
// fetch from livescore.com (does not work)
const res = await fetch('https://prod-public-api.livescore.com/v1/api/react/date/soccer/20210803/1.00')
const json = await res.json()
console.log(json)
} catch (error) {
console.log(error.message)
}
try {
// fetch from localhost:3000 (works great)
const res = await fetch('http://localhost:3000')
const json = await res.json()
console.log(json)
} catch (error) {
console.log(error.message)
}
}
main()
</script>
</body>
</html>
import express from 'express'
import cors from 'cors'
import fetch from 'node-fetch'
const app = express()
const port = 3000
app.use(cors())
app.get('/', async (req, res) => {
const data = await fetch('https://prod-public-api.livescore.com/v1/api/react/date/soccer/20210803/1.00')
const json = await data.json()
res.json(json)
})
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})
I'm trying to access an external
API
but this error occurs, is there any way around this problem?has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.