HTTP GET and POST Request Handling in ExpressJs

In this post i am going to explain you two very important http request GET and POST, This tutorial will help yo to understand GET and POST HTTP request in expressjs

We can handle GET request by using app.get() function and POST request by using function in expressjs where app is the object of express module, Also for receiving form data by post method, We need to include one additional module called body-parser in our project.

So lets start tutorial step by step.

Here this example will so you GET and POST request by sending email in nodejs.

Node Emailer

Node Emailer is program to send email in nodejs here i have created a simple html form which will load when HTTP GET Request Make and send form details to server by HTTP POST request using jquery and POST data handled by express.

Create package.json file.


  "name": "NodeJs-Emailer",
  "version": "0.0.1",
  "description": "NodeJs email form to send email using nodejs",
  "dependencies": {
    "nodemailer": "~0.7.1",
    "express": "~3.4.0",
    "body-parser": "~1.13.1"

Create html email form which will load while get request make.


<!DOCTYPE html>
<html lang="en">
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Send email in nodejs</title>
<link rel="stylesheet" href="">
<div class="panel panel-primary" style="width:50%;margin:0 auto; margin-top:10%">
<div class="panel-heading"><h3>Email Form In Node.Js</h3></div>
<div class="panel-body" style="height:40%; text-align:center;" >
<p class="bg-info" id="msg"></p>
 <form class="form-horizontal" role="form" id="emailForm" method="post">
  <div class="form-group">
    <label class="control-label col-sm-2" for="email">Email:</label>
    <div class="col-sm-10">
      <input type="email" class="form-control" name="email" placeholder="Enter email" required="required">
  <div class="form-group">
    <label class="control-label col-sm-2" for="subject">Subject:</label>
    <div class="col-sm-10">
      <input type="text" class="form-control" name="subject" placeholder="Enter subject" required="required">
  <div class="form-group">
    <label class="control-label col-sm-2" for="description">Description:</label>
    <div class="col-sm-10">
      <textarea class="form-control" name="description" placeholder="Enter Description"></textarea>
  <div class="form-group">
    <div class="col-sm-offset-2 col-sm-10">
      <button id="send" class="btn btn-primary btn-lg" type="button">
         <span class="glyphicon glyphicon-send" ></span> Send
 <script src=""></script>
 <script src=""></script>
  var fullUrl =  location.protocol+'//'+location.hostname+(location.port ? ':'+location.port: '');
      var formData = $("#emailForm").serialize();
      $("#msg").text("Email sending Please wait..");
                url: fullUrl+'/send',
                type: 'POST',
                data: formData,
                success: function(result) {
                error: function(e) {
                                 $("#msg").empty().text("There is some error to send email, Error code:"+e.status +", Error message:"+e.statusText);
                dataType: "html",
                timeout: 60000

In Above code i have serialize form data using jquery and sent details to server by HTTP POST request.

Now finally create your app.js file which will handle your all GET and POST request.


var http=require('http');
var express=require('express');
var nodemailer = require("nodemailer");
var bodyParser = require('body-parser')
var app=express();
var port = Number(process.env.PORT || 5000);
  extended: true

});'/send', function(req, res){
if( == "" || req.body.subject == "") {
  res.send("Error: Email & Subject should not blank");
  return false;

    from: "Node Emailer ✔ <[email protected]>", 
    subject: req.body.subject+" ✔", 
    //text: "Hello world ✔", 
    html: "<b>"+req.body.description+"</b>" 
res.send("Email has been sent successfully");

var server = http.createServer(app).listen(port, function() {
console.log("Listening on " + port);

In above code you can see both example of HTTP request.

Now run your application

 $ node app.js

Node Emailer
Node Emailer

You can see the working demo by clicking on demo button and download source code.

Hope this tutorial will help you to understand HTTP GET and POST request in expressjs

If you like this post please don’t forget to subscribe My Public Notebook for more useful stuff.