в html в строке 33 должно быть еще одно заклинание типа .then(response => response.json())
--
const path = require('path')
const express = require("express");
const app = express();
const jsonParser = express.json();
app.use(express.static(path.join(__dirname, 'public')))
app.post("/", jsonParser, function (request, response) {
console.log(request.body);
if (!request.body) return response.sendStatus(400);
response.json(request.body);
});
app.listen(8081, () => {
console.log("e");
});
<!DOCTYPE html>
<html>
<head>
<title>Регистрация</title>
<meta charset="utf-8" />
</head>
<body>
<h1>Введите данные</h1>
<form name="registerForm">
<label>Имя</label></br>
<input type="text" name="userName" /></br></br>
<label>Возраст</label></br>
<input type="number" name="userAge" /></br></br>
<button type="submit" id="submit">Отправить</button>
</form>
<script>
document.getElementById("submit").addEventListener("click", function (e) {
e.preventDefault();
let registerForm = document.forms["registerForm"];
let userName = registerForm.elements["userName"].value;
let userAge = registerForm.elements["userAge"].value;
let user = JSON.stringify({ userName: userName, userAge: userAge });
fetch("/",
{
method: "POST",
headers: {
'Content-Type': 'application/json;charset=utf-8'
},
body: user
})
.then((response) => response.json())
.then((value) => {
console.log(value);
})
});
</script>
</body>
<html>