I encountered a CORS error (CORS request rejected: https://localhost:3000/users) while attempting to register a new user. This issue arose from content in the book Building APIs with node.js, Chapter 12. I am currently using Firefox on Ubuntu and have tried various solutions online to disable CORS within Firefox without success. I am unsure whether I need to adjust any settings in Firefox or add specific code to the example provided. Any guidance would be greatly appreciated.
import NTask from "../ntask.js";
import Template from "../templates/signup.js";
class Signup extends NTask {
constructor(body) {
super();
this.body = body;
}
render() {
this.body.innerHTML = Template.render();
this.body.querySelector("[data-name]").focus();
this.addEventListener();
}
addEventListener() {
this.formSubmit();
}
formSubmit() {
const form = this.body.querySelector("form");
form.addEventListener("submit", (e) => {
e.preventDefault();
const name = e.target.querySelector("[data-name]");
const email = e.target.querySelector("[data-email]");
const password = e.target.querySelector("[data-password]");
const opts = {
method: "POST",
url: `${this.URL}/users`,
json: true,
body: {
name: name.value,
email: email.value,
password: password.value
}
};
this.request(opts, (err, resp, data) => {
if (err || resp.status === 412) {
alert("IF: " + err);
this.emit("error", err);
} else {
alert("ELSE")
this.emit("signup", date);
}
});
});
}
}
module.exports = Signup;