When you send data via a form to another route, the way you retrieve your request data (whether sent via POST or GET) with expressjs is using the syntax:
req.body.inputname
Where inputname
is the name value of the input.
For example, in the code below:
Example
yourproject/views/index.ejs
<html>
<head><title></title></head>
<body>
<form action="/" method="POST">
<input name="abc" />
<input type="submit" value="submit"/>
</form>
</body>
</html>
yourproject/routers/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.post("/", async function(Req, res, next)
{
res.send(req.body.abc)
});
module.exports = router;
We simply send to the route "/"
(this means we add nothing after the domain name) the input named abc
's value.
And then we display that value on the screen.
Example
In the example below, we send the text "Hi world" through our form in the first image by pressing submit
and if done properly, we will display the second image in the browser as below.