I am working on a registration form in HTML. The form uses script to take the data entered by user, and turn it into JSON format so that my backend can process it. My text editor is Visual Studio Code and I am checking the functionality of my code with Five Server extension. When I run the code, I get a 405 (method not allowed) message whenever I try to submit data. I suspect that when I make the fetch call, I am not properly referencing the resource. Does my JavaScript file need to be in the same folder as my HTML file? Or is my issue something else?
<script>
const form = document.getElementById('createAccount')
form.addEventListener('submit', registerUser)
// Will send Data as JSON
async function registerUser(event) {
event.preventDefault()
const firstName = document.getElementById('signupFname').value
const lastName = document.getElementById('signupLname').value
const email = document.getElementById('signupEmail').value
const username = document.getElementById('signupUsername').value
const password = document.getElementById('signupPword').value
//Is this right?
const result = await fetch('/signup', {
method: 'POST',
headers: {
'Content-Type':'application/json'
},
body: JSON.stringify({
firstName,
lastName,
email,
password,
username
})
}).then((res) => res.json())
console.log(result)
}
</script>
Here is my Javascript
const express = require("express");
const router = express.Router();
const path = require('path');
router.use('/', express.static(path.join(__dirname, 'static')));
// mongodb user model
const User = require("./../models/users");
// Password handler
const bcrypt = require("bcryptjs");
// Signup
router.post("/signup", (req, res) => {
let { fname, lname, username, password, email } = req.body;
fname = fname.trim();
lname = lname.trim();
username = username.trim();
password = password.trim();
email = email.trim();
if()
// Trimmed code for sake of space
)};