request.query is used only when the URL has parameters and you wish you to obtain those parameter value.
Hereās how I would rewrite your code to make it work:
function post(url, data) {
return fetch(url, {
method: "POST",
body: data,
headers: {
'Content-Type': 'application/json'
}
});
}
form.addEventListener("submit", event => {
let name = form.elements["Username"].value;
let pass = form.elements["Password"].value;
let url = "/CreateAccount";
let credentials = {name:name, pass:pass}
// let serialized = name + ":" + pass
post("https://response-test.glitch.me/CreateAccount", credentials);
console.log("client.js: sent", credentials)
// sendData(serialized)
});
Then on the server side:
app.use(express.json());
app.post("/CreateAccount", function(request, response) {
console.log("data received")
let req = request.body;
let name = request.body.name; // the name that was sent through the post request
let password = request.body.pass // the password that was sent through the post request
// Do stuff with these values and optionally send a response with response.send
});
Didnāt offer much help on how to circumvent the issue in my case, it did give me the idea of sending different data over though and that the data Iām sending might be in invalid format; Iāll try that, thanks.
Either the data was not converted to JSON properly or the data had syntax errors due to how it was formatted, how do I fix this?
Itās not being parsed to JSON properly I think.
Edit: fixed it! I figured Iād use JSON.stringify to convert it to a JSON string before sending, it worked! Thanks for the help, @khalby786 and RiversideRocks.