the server sends a response. I don't understand how to process it. If you get "json" opens a page with array. If you get "res.render" refreshes the page. How can I get json and process it.
// express.js
if (!errors.isEmpty()) {
if (errors.array().find(el => el.param === 'login')) {
//1 version
res.status(409).json({
message: 'Error login'
})
//2 version
res.render('reg', {
isReg: true
})
}
}
<!-- Handlebars -->
<form class="login-form" action="/reg" method="POST" >
<div class="form-group">
<label for="exampleInputEmail1" class="text-uppercase">Login</label>
<input type="text" class="form-control" id="validationLogin" name="login" placeholder="" required>
</div>
<div class="block-login d-flex justify-content-center">
<button type="submit" class="btn btn-login float-right">Submit</button>
</div>
</form>
<!-- how to get an answer? -->
<script>
//example
var answer = res.json
</script>
If you use res.json then the end point will return JSON. This is designed to be consumed with some custom code and not by being loaded directly into the browser. You could make an Ajax request and then insert data from the result into the DOM. This is how you get the result without loading a new page.
If you use res.render then then end point will return whatever you render, which is almost always HTML. This is a new page. You are expected to pass the data as the second argument to render and then process it with the view engine you have selected (handlebars in this case).
e.g.
<p>{{isReg}}</p>