Reputation: 445
I have this fetch request to get some auth data from my form to the server
const registerUser = async (e) => {
e.preventDefault()
const userName = document.getElementById('username').value
const password = document.getElementById('password').value
const confirm = document.getElementById('password-confirm').value
//Make this better with DOM and css later
if (password !== confirm){
alert('Passworfds dont match')
}
const result = await fetch('/api/register', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
username,
password
})
}).then((res) => res.json())
}
The server code to accept this is so far nothing other than this
app.post('/api/register', async (req, res) => {
console.log(req.body)
res.json({ status: 'ok' })
})
When i hit the submit button i am getting a POST - 405 - method not allowed error in the console if anyone can please help?
Upvotes: 0
Views: 2270
Reputation: 445
This was slightly foolish and a really simple mistake.
My severs serves my html and the login form was running on live server through VScode on the wrong port. A Simple routing issue I have now solved!
Upvotes: 1
Reputation: 157
Do you have installed the Body-Parser?
var bodyParser = require("body-parser");
var jsonParser = bodyParser.json();
app.post("/api/register", jsonParser, (req, res) => {
console.log(req.body);
res.json({ status: "ok" });
});
Upvotes: 0