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,
Table of Contents
ToggleInstall dependencies
Will going to use following node modules for handling session.
- Express 4
- Body parser
- 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",
"mysql":"^2.5.5",
"body-parser":"^1.12.0"
}
}
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,
app.get('/fetchdata',function(req,res){
var data = {
"Data":""
};
connection.query("SELECT * from testing",function(err, rows, fields){
if(rows.length != 0){
data["Data"] = rows;
res.json(data);
}else{
data["Data"] = 'No data Found..';
res.json(data);
}
});
});
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,
app.post('/login',function(req,res){
var email = req.body.email;
var pass = req.body.password;
var data = {
"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..";
res.json(data);
}else{
data["Data"] = "Email or password is incorrect.";
res.json(data);
}
});
});
Using “app.post(“/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.email & 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 : express, express-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″.