Loading

Node.js

How do you handle a post request in node JS?. The Complete Node.js Developer Course 2022 [Videos].

Node.js is a platform built on Chromes JavaScript runtime for easily building fast and scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices

POST is a request method supported by HTTP used by the World Wide Web. The HTTP POST method sends data to the server. The type of the body of the request is indicated by the Content-Type header. We use Express.js in order to create a server and to make requests (GET, POST, etc).

npm i express

Note: The npm in the above commands stands for node package manager, a place from where we install all the dependencies.

So in order to use Express to address POST requests on our server, we use the app.post method and then we specify the route, and we have a callback.

app.post(route, function(req, res){
    //this is a callback function
})

Note: If you are going to make GET, POST request frequently in NodeJS, then use Postman , Simplify each step of building an API.

In this syntax, the route is where you have to post your data that is fetched from the HTML. For fetching data you can use bodyparser package.

Web Server: Create app.js in the root folder. Create your server as shown in the below example.

const express = require("express");
const app = express();
  
// Define routes here ...
  
app.listen(3000, function(){
  console.log("server is running on port 3000");
})

Handle Post Request: Here you will learn how to handle HTTP POST request and get data from the submitted form.
 

Create index.html in the root folder of your application and write following HTML code in it.

Filename: index.html 
 

<!DOCTYPE html>
<html lang="en" dir="ltr">
  
<head>
    <meta charset="utf-8">
    <title>Calculator</title>
</head>
  
<body>
    <h2>Simple Calculator.</h2>
    <form action="/" method="post">
        <input type="text" name="num1" 
            placeholder="First Number">
        <input type="text" name="num2" 
            placeholder="Second Number">
              
        <button type="submit" name="submit">
            calculator
        </button>
    </form>
</body>
  
</html>

See All

Comments (126 Comments)

Submit Your Comment

See All Posts

Related Posts

Node.js / Youtube

How to Create Node.js Web Server?

Node.js is a platform built on Chromes JavaScript runtime for easily building fast and scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices
27-jan-2021 /3 /126

Node.js / Youtube

What is Node.js?

Node.js is a platform built on Chromes JavaScript runtime for easily building fast and scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices
27-dec-2020 /3 /126

Node.js / Youtube

Setting up the development environment of Node.js.

Node.js is a platform built on Chromes JavaScript runtime for easily building fast and scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices
9-Jun-2021 /3 /126