40
loading...
This website collects cookies to deliver better user experience
https://www,facebook-clone.com
, the protocol is https://
, the host is www.facebook-clone.com
, and the hidden port number is 443 (the port number typically used for https).Origin: http://localhost:3000
Access-Control-Allow-Origin
key, to specify which origins can access the server’s resources. The key will have one of two values:Access-Control-Allow-Origin: http://localhost:3000
Access-Control-Allow-Origin: *
Access-Control-Allow-Origin
value from the server. If the frontend domain does not match the value, the browser raises the red flag and blocks the API request with the CORS policy error.Access-Control-Allow-Origin: *
header to the response.https://cors-anywhere.herokuapp.com/https://joke-api-strict-cors.appspot.com/jokes/random
https://joke-api-strict-cors.appspot.com/jokes/random
from the url above. Then it makes the request to get that server’s response. And finally, the proxy applies the Access-Control-Allow-Origin: *
to that original response.https://joke-api-strict-cors.appspot.com/
from above:const express = require('express');
const request = require('request');
const app = express();
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
next();
});
app.get('/jokes/random', (req, res) => {
request(
{ url: 'https://joke-api-strict-cors.appspot.com/jokes/random' },
(error, response, body) => {
if (error || response.statusCode !== 200) {
return res.status(500).json({ type: 'error', message: err.message });
}
res.json(JSON.parse(body));
}
)
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => console.log(`listening on ${PORT}`));
Access-Control-Allow-Origin: *
header to every response from the server. At its own jokes/random GET endpoint, the proxy requests a random joke from another server. The same-origin policy doesn’t step in to block the request, even though the domains are different. After all, this is a server-to-server request. Finally, the proxy creates a response to the original requester (an app on the browser) consisting of the resulting data and the middleware-applied Access-Control-Allow-Origin: *
header.