Skip to main content

Command Palette

Search for a command to run...

What Are Requests In Express?

Updated
1 min read
What Are Requests In Express?
E

I'm a full-stack web developer who loves building new projects and learning new things

It might not show from my posts, but I'm also quite funny.

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>

form data send 0.png

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 form send data express 1.png

and if done properly, we will display the second image in the browser as below.

form send data express 2.png

More from this blog

Javasper

50 posts

I'm a full stack software developer who loves to building new projects and solving difficult problems.

It might not be obvious from my posts, but I'm actually quite funny.