By Team Clofus Innovations | Mon Jan 17 2022
Node js is a open source, server side( back end) java-script platform which is used to connect client side with the server database. It is single threaded, Non-blocking I/O (Each line is executed separately irrespective of depending upon the other inputs). Before proceeding download and configure node js. (Check in setup development environment for installation procedure).
Express is a very popular web application framework built to create Node JS Web based applications. Install express in the local computer by the following command
npm install express --save
Above command saves installation locally in
node_modules directory and creates a directory express inside node_modules.
File name : server.js
var express = require('express'); //Express framework included in the server.js file
var app = express();
app.get('/', function (req, res) {
res.send('Hello World');
})
app.listen(3000); // server listening port
Note : Save this file as a server.js open command prompt enter node server.js to run the server. (configure in nginx for the client side )
Express application makes use of a callback function whose parameters are request and response objects.
app.get('/', function (req, res) {
// --
})
Response Object - The response object represents the HTTP response that an Express app sends when it gets an HTTP request.
Routing refers to determining how an application responds to a client request to a particular endpoint, which is a URI (or path) and a specific HTTP request method (GET, POST, PUT, DELETE)
This can be done by using several methods. They are called as Rest Architecture. What is REST architecture?
REST stands for REpresentational State Transfer. REST is web standards based architecture and uses HTTP Protocol. A REST Server simply provides access to resources and REST client accesses and modifies the resources using HTTP protocol. It usually done using json fromat
1)GET Method - This is used to provide a read only access to a resource.
2)PUT Method - This is used to create a new resource.
3)POST Method - This is used to update a existing resource or create a new resource.
4)DELETE Method - This is used to remove a resource.
app.get('/mainpage', function(req, res){
//This is the get method which will Route to the mainpage
// The below line requesting data from mysql database
connection.query('SELECT * FROM tablename', function(err, rows){
console.log("data send to frontpage");
res.send(rows); //It will send response to corresponding http method in angular js
});
app.post('/mainpage', function(req, res){
// The below line requesting data from mysql database
connection.query('SELECT * FROM tablename', function(err, rows){
console.log("data send to frontpage");
res.send(rows); //It will send response to corresponding http method in angular js
});
//This will insert the data
app.put('/mainpage', function(req, res){
// The below line inserting data from mysql database
connection.query('insert into tablename value ('name')', function(err, rows){
});
//This will delete the data in the resource
app.delete('/mainpage', function(req, res){
//This is the delete method which will Route to the mainpage
// The below line deleting data from mysql database
connection.query('Delete from tablename', function(err, rows){
});
This Res.send function is used to send the data to client side http method of Angular Js.
For Example:
app.post('/mainpage', function(req, res){
res.send('Hello world'); //It will send response to corresponding http method in angular js
});
This res.send(‘Hello world’) will send the hello world text to the client side http method of the angular js.
Send the data from client side to server side by using query parameter.
For Example:
Clent side request app.controller('mainctl',function($scope,$http,$routeParams){
$http({
method: 'GET',
url:'http://localhost:2901/mainpage',
params: { //query parameter send data with the help of params
urname(this is key):$routeParams.uid(this is value) //params data will attach as query like http://localhost:2901/mainpage?urname='data'
}
}).then (function(res){
console.log(res.data);
})
})
Server side receiving request and response to client.
app.get('/mainpage', function(req, res){ // request received from client side
console.log("using Query")
console.log(req.query); //to access the query parameter use(req.query or req.query.urname(use key))
reg.find(req.query,function(err,data){ //request sent to mongodb and result will be stored in data parameter
res.send(data); //response will send to client request.
});
});
Send the data from client side to server side by using Params.
For Example:
Client Side Request: app.controller('mainctl',function($scope,$http,$routeParams){
$http({
method: 'GET',
url:'http://localhost:2901/mainpage/'+$routeParams.uid(this is value) // value will send to server through url
}).then (function(res){
console.log(res.data);
})
})
Server side receiving request and response to client.
app.get('/mainpage/:urname', function(req, res){ // request received from client side
console.log("using Param")
console.log(req.params); //to access the query parameter use(req.params or req.params.urname)
reg.find(req.query,function(err,data){ //request sent to mongodb and result will be stored in data parameter
res.send(data); //response will send to client request.
});
});
Body is used to send data securely(i.e., hide information and send a request to server). Important things to follow while using body parameter. install body-parse.use(bodyParser.json()) - after using this line only body will accept body request from client.
For Example:
Client side:app.controller('signinctrl',function($scope,$http,$location){
$scope.send data=function(user){ //user input all store in json type
$http({
method: 'POST',
url: 'http://localhost:2901/signin',
data:user // data information pass through body parameter
}).then(function(res) {
console.log(res.data);
})
}
})
Server Side:
var express = require('express'); //using express package
var bodyParser = require('body-parser'); // using body-parser package without this data will not work in body parameters
var mongoose = require('mongoose'); // using mongoose package
var app = express();
app.use(bodyParser.json()); //access for json files
app.post('/signin',function(req,res){
console.log("Using Body Method")
console.log(req.body); // access body information using req.body
reg.find(req.body,function(err,data){ //request result will store in data parameter
res.send(data); // response will send to client resquest
});
})
Header is used give access control for client side request and response.
For Example:
app.all('*',function(req,res,next){ //*
represent access control to all app using
methods.
console.log("Allow header access");
res.header('Access-Control-Allow-Origin', '*'); //origin access control
res.header('Access-Control-Allow-Methods', 'GET,POST,PUT,DELETE'); //method access control
res.header('Access-Control-Allow-Headers', 'Content-Type'); // header access control (json, text/plain,..)
next();
})