In this tutorial we will learn how to handle GET & POST request in node.js using Express 4. Using Express 4 we can easily handle requests. And we have also used Mysql to Fetch and post data from database.

SQL file can be found along with the downloaded code.


Let’s start with installing dependencies and then the code,

Install dependencies

Will going to use following node modules for handling session.

  1. Express 4
  2. Body parser
  3. Mysql

I have created a package.json file that will install all required dependencies. have a look,

"name": "GET-POST-Request",
"version": "0.0.1",
"main": "server.js",
"dependencies": {
        "express": "^4.8.7",


How to install dependencies?

By typing following command you can install dependencies,

npm install

Note:You must also have MySQL installed if you are working locally.

Initialization & Require

First will include require node modules and http server.
Also we have done connection of Mysql for GET & POST data.

var app   = require('express')(); // Express App include
var http = require('http').Server(app); // http server
var mysql = require('mysql'); // Mysql include
var bodyParser = require("body-parser"); // Body parser for fetch posted data
var connection = mysql.createConnection({ // Mysql Connection
        host     : 'localhost',
        user     : 'root',
        password : '',
        database : 'abc',
app.use(bodyParser.urlencoded({ extended: false })); 
app.use(bodyParser.json()); // Body parser use JSON data


GET Request

In GET request we are basically fetching data/values from database and showing as JSON format.See below code,

    var data = {
    connection.query("SELECT * from testing",function(err, rows, fields){
        if(rows.length != 0){
            data["Data"] = rows;
            data["Data"] = 'No data Found..';

Using “app.get(“/url”,function)” we can do URL routing and based on that will fetch data as required.

And “connection.query(“Query”,function)” will used for writing query and in callback will get required data as you can see above code.


POST Request

In POST request we are posting data/values and based on that we will validate and send response as JSON.
I’ve created a login API for demo purpose. See below code,

    var email =;
    var pass = req.body.password;
    var data = {
    connection.query("SELECT * from login WHERE email=? and password=? LIMIT 1",[email,pass],function(err, rows, fields){
        if(rows.length != 0){
            data["Data"] = "Successfully logged in..";
            data["Data"] = "Email or password is incorrect.";

Using ““/url”,function)” we can do URL routing and based on that will fetch or post data as required.

And First will get posted data by using “req.body” (i.e : & req.body.password ).

Now use that data in query to check whether user is exist or not and based on that it will return response.


How to run?

First check whether you have installed Node.js or not.

Then install all required dependencies (i.e : expressexpress-session & body-parser ) by typing following command in terminal,

npm install

Now, run the server using following command.

node server.js

And now go to browser and hit “localhost:8080″.


Watch Video tutorial