I'm learning javascript and express. I have already made backend applications that worked correctly, but I don't know how to send a request from a browser and receive the response from the server. This is a simple example of what I have tried:
server.js:
const express = require("express");
const app = express();
app.listen(3000, () => {
console.log("Listening on port 3000!")
})
app.get("/", (req, res) => {
res.send("Hello");
})
The server is on the glitch.com platform. When I go to the https://expressjs-test.glitch.me/ project from my browser, I see the Hello
message in the HTML document (it works correctly).
Javascript file executed from my browser:
let req = fetch("https://expressjs-test.glitch.me/");
req.then(response => console.log(response));
The problem I get when I try to do this is:
Access to fetch at 'https://expressjs-test.glitch.me/' from origin 'null' 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.
I've seen other questions on stackoverflow and other sites but didn't understand how to fix this cors error.
Thank you for your time. Sorry if I made a mistake in this post.