I have a start_express.js
file and I run it with node:
const express = require("express");
const app = express(),
DEFAULT_PORT = 5000
app.set("port", process.env.PORT || DEFAULT_PORT);
app.get("/whatever", function (req, res) {
res.send("It works!");
console.log("Something's wrong");
});
var server = app.listen(app.get("port"));
I my react app, I have a handleSubmit
method that tries to get that response:
handleSubmit(event) {
axios.get('http://127.0.0.1:5000/whatever')
.then(function (response) {
alert(response);
})
.catch(function (error) {
console.log(error);
});
event.preventDefault();
}
When I click on submit, the start_express server prints "Something's wrong", but the alert on the client never works. It gives me the following error:
Failed to load http://127.0.0.1:5000/whatever: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access.
How am I supposed to send requests from the client if I can't respond?