How to receive post parameter in Express.js ?
Last Updated :
08 Jan, 2025
Express is a small framework that sits on top of Node.js’s web server functionality to simplify its APIs and add helpful new features. It makes it easier to organize your application’s functionality with middleware and routing; it adds helpful utilities to Node.js’s HTTP objects; it facilitates the rendering of dynamic HTTP objects. Express.js provides various types of methods for handling different types of incoming requests such as get, Post, etc from the client-side. In this article, we will discuss how to receive post parameter in express.js.
POST parameter can be received from a form using express.urlencoded() middleware and the req.body Object. The express.urlencoded() middleware helps to parse the data that is coming from the client-side.
Syntax:
express.urlencoded( [options] )
Parameter:
The options parameter contains various properties like extended, inflate, limit, verify, etc.
Return Value:
It returns an Object.
Example: Let's discuss each step one by one to receive post parameters in the express.js
Step 1: Create an "app.js" file and initialize your project using npm.
npm init
Step 2: Create an "index.html" file and install the express package using npm.
npm install express
Project Structure
Project/File StructureStep 3: Now let us first code the "index.html" file. In it, we make a form and submit that form as method "POST" to the '/submit' route which we would declare later in the "app.js" file.
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible"
content="IE=edge">
<meta name="viewport" content=
"width=device-width, initial-scale=1.0">
</head>
<body>
<!-- Action to the same route as in app.post() method -->
<form action="/submit" method="post">
Name: <input type="text" name="name">
<br><br>
Email: <input type="email" name="email">
<br><br>
Gender: <br> <input type="radio" name="gender" id="male">
Male
<br>
<input type="radio" name="gender" id="female">
Female
<br>
<input type="radio" name="gender" id="private">
Don't want to disclose
<br><br>
Hobbies: <br> <input type="checkbox"
name="painting" id="painting">
Painting
<br>
<input type="checkbox" name="dancing" id="dancing">
Dancing
<br>
<input type="checkbox" name="singing" id="singing">
Singing
<br> <br>
<input type="file" name="file" id="">
<br> <br>
<button type="submit">Submit</button>
</form>
</body>
</html>
Step 4:
- Now in the app.js file we will define the GET request to the root of the app. For the GET request, we send the "index.html" file to the client.
- For the '/submit' POST request we take input from the form and console log req.body. The req.body is an Object which contains all the attributes of the form. We can access any attribute using req.body.<attribute_name>. For eg: req.body.name in this case.
app.js
// Requiring express npm package
const express = require('express')
const app = express()
// Using express.urlencoded middleware
app.use(express.urlencoded({
extended: true
}))
// Handling Post request
app.post('/submit',function(req,res){
// Can access all parameters from req.body
console.log('POST parameter received are: ',req.body)
res.redirect('/')
})
// Get request
app.get('/',function(req,res){
// Sent index.html file to the client
res.sendFile(__dirname+'/index.html')
})
// Creating server at port 3000
app.listen(3000,function(req,res){
console.log('started listening at server 3000')
})
Step 5: Run app.js file using below command:
node app.js
Step 6.Type localhost server address in any browser.
http://localhost:3000/
Output: So, In this way we can receive post parameter in the express.js.
Similar Reads
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read
Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
React Tutorial React is a powerful JavaScript library for building fast, scalable front-end applications. Created by Facebook, it's known for its component-based structure, single-page applications (SPAs), and virtual DOM,enabling efficient UI updates and a seamless user experience.Note: The latest stable version
7 min read
JavaScript Interview Questions and Answers JavaScript is the most used programming language for developing websites, web servers, mobile applications, and many other platforms. In Both Front-end and Back-end Interviews, JavaScript was asked, and its difficulty depends upon the on your profile and company. Here, we compiled 70+ JS Interview q
15+ min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read