Calling post of form data.

This commit is contained in:
Bradley Shellnut 2021-03-03 20:11:37 -08:00
parent fb671e0b6f
commit 3b15223b1d
2 changed files with 36 additions and 0 deletions

View file

@ -17,6 +17,10 @@ async function startApp() {
root: path.join(__dirname, "public"),
})
app.post('/api/register', {}, (request, reply) => {
console.log('request', request.body.email, request.body.password);
})
// app.get("/", {}, (request, reply) => {
// reply.send({
// data: "hello world",

View file

@ -8,5 +8,37 @@
</head>
<body>
<h1>Hello</h1>
<h3>Register Form</h3>
<form id="register-form">
<input type="email" name="email">
<input type="password" name="password">
<button type="submit">Register</button>
</form>
<script>
;(() => {
const form = document.getElementById("register-form")
form.addEventListener("submit", async (e) => {
e.preventDefault();
try {
const values = Object.values(form).reduce((obj, field) => {
if (field.name) {
obj[field.name] = field.value
}
return obj
}, {})
const res = await fetch('/api/register', {
method: "POST",
body: JSON.stringify(values),
headers: { "Content-type": "application/json; charset=UTF-8"},
})
console.log("values", values)
} catch (e) {
console.error(e)
}
})
})()
</script>
</body>
</html>