Tried to implement a register template with nodejs, console logs the error "POST is not defined".
Here index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8"/>
<meta name="viewpost" content="width=device-width, initial-scale=1.0"/>
<title>Login & Register with NodeJS</title>
</head>
<body>
<form id="reg-form">
<input type="text" autocomplete="off" placeholder="Username" id="username">
<input type="password" placeholder="Password" id="password">
<input type="submit" value="Submit Form">
</form>
<script>
const form = document.getElementById('reg-form')
form.addEventListener('submit', registerUser)
async function registerUser(event) {
event.preventDefault()
try {
const username = document.getElementById('username').value
const password = document.getElementById('password').value
}
catch(e) {
console.log( "Assignment didn't work " + e.message)
}
try {
const result = await fetch('./api/register', {
method: POST,
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
username,
password
}).then((res) => res.json())
})
console.log(result)
} catch(e) {
console.log(e.message)
}
}
</script>
</body>
</html>
here the index.js:
const express = require('express')
const path = require('path')
const app = express()
app.use('/', express.static(path.join(__dirname, 'static')))
app.use(express.json())
app.post('./api/register', async (req, res) => {
console.log(req.body)
res.json({status: 'ok'})
})
app.listen(9999, () => {
console.log('Server up at 9999')
})
My Server is up on localhost and running, the form is working as well, but it seems as if the POST method can not be called. I didn't connect any database just yet. I'm running on Windows 10. Can somebody tell me why it isn't working?