I know this might be a simple question and has a tutorial somewhere but I can't seem to find it.
I generated an app using express-generator
and i added a simple form to a route
views/form.ejs
<div>
<h1>This is <%= name %></h1>
<form action="/form" method="POST">
<input type="text" name="question" placeholder="question"><br>
<input type="text" name="answer" placeholder="answer"><br>
<input type="submit">
</form>
what i am trying to do is process the request after the form submission without doing anything to the page. Basically I want it to stay the same so I tried the following
routes/index.js
var express = require('express');
var router = express.Router();
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: 'Express' });
});
router.get('/form', function (req, res) {
res.render('form', {name: "My Name"});
});
router.post('/form', function (req, res) {
console.log(req.body);
});
module.exports = router;
what happens is that the browser keeps waiting for a response from the server. I tried adding next()
but it gives an error, I also tried returning from the function with no use.
So how can I make a post request or handle any route in general without responding to the client?