Advance Express Concept
In this lecture we covered the light green highlighted portion from below diagram.
Till last section we seen that how to pass data through params in this section we will see how to send data through headers & body, middleware , status code and response type.
Data through headers
- step-1 Add the below line inside your route to use the headers value.
/* var lastIndex=req.query.counter */
console.log(req.headers) // logging header
var lastIndex=req.headers.counter // getting counter value which passed through headers
- step-2 Now run the code using
node index
and pass value into headers using Postman.
- Log of above code
{
counter: '10',
'user-agent': 'PostmanRuntime/7.36.1',
accept: '*/*',
'postman-token': 'b24f77d1-78b8-4e74-a4c9-9e682462d449',
host: 'localhost:3000',
'accept-encoding': 'gzip, deflate, br',
connection: 'keep-alive'
}
- Response
Calculated Sum is : 55
Data through body
- step- 1 add the below code into your route to use the body value
console.log(req.body);
var lastIndex = req.body.counter;
console.log(lastIndex);
and send below body through Postman.
{
"counter":10
}
and you will see below error after hitting the send button.
undefined
TypeError: Cannot read properties of undefined (reading 'counter')
from here middleware came into the picture.
Middleware
Express is a routing and middleware web framework that has minimal functionality of its own. An Express application is essentally a series of middleware function calls.
- Middleware functions are function that have access to the request object(req), the response objeect(res) and the next middleware function in the applications request-response cycle. The next middleware function is commonly denoted by a variable named next.
Middlleware function can perform the following task.
- Execute any code.
- Make changes to the request and the response objects.
- End the request /response cycle.
- Call the next middleware function in the stack
Example
- code
const express = require("express");
const app = express();
const port = 3000;
function middleware1(req,res,next){
console.log("from inside middleware" + req.headers.counter)
next();
}
app.use(middleware1);
function calculateSum(counter) {
var sum = 0;
for (var i = 0; i <= counter; i++) {
sum = sum + i;
}
return sum;
}
function handleFirstRequest(req, res) {
/* console.log(req.query);
var lastIndex = req.query.counter;*/
console.log(req.headers);
var lastIndex = req.headers.counter;
// console.log(req.body);
// var lastIndex = req.body.counter;
console.log(lastIndex);
var calculatedSum = calculateSum(lastIndex);
var answer = "Calculated Sum is : " + calculatedSum;
console.log(answer);
res.send(answer);
}
app.get("/handlesum", handleFirstRequest);
app.listen(port, () => {
console.log(`Example app listening on port ${port}`);
});
- Terminal Output
from inside middleware10
{
counter: '10',
'content-type': 'application/json',
'user-agent': 'PostmanRuntime/7.36.1',
accept: '*/*',
'postman-token': 'f10c8d3f-b60e-4929-887f-269529503dc3',
host: 'localhost:3000',
'accept-encoding': 'gzip, deflate, br',
connection: 'keep-alive',
'content-length': '22'
}
10
Calculated Sum is : 55
Again Sending data through body using middleware
- Step-1 Add dependency of middleware in express.
npm i body-parser
- step-2 write below code in index.js file using middleware
const express = require("express");
const app = express();
const bodyParser=require("body-parser") //importing middleware
const port = 3000;
app.use(bodyParser.json()) // middleware
function calculateSum(counter) {
var sum = 0;
for (var i = 0; i <= counter; i++) {
sum = sum + i;
}
return sum;
}
function handleFirstRequest(req, res) {
/* console.log(req.query);
var lastIndex = req.query.counter;*/
// console.log(req.headers);
// var lastIndex = req.headers.counter;
console.log(req.body);
var lastIndex = req.body.counter;
console.log(lastIndex);
var calculatedSum = calculateSum(lastIndex);
var answer = "Calculated Sum is : " + calculatedSum;
console.log(answer);
res.send(answer);
}
app.get("/handlesum", handleFirstRequest);
app.listen(port, () => {
console.log(`Example app listening on port ${port}`);
});
-
step-3
node index.js
-
Output
Calculated Sum is : 55
Status code
HTTP response status codes.
HTTP response status code indicate whether a specific HTTP request has been successfully completed. Response are grouped in five classes.
- Information response (100-199)
- Successful response (200-299)
- Redirecting message (300-399)
- Client Error Response (400-499)
- Server Error Response (500-599)
function handleFirstRequest(req,res){
var counter =req.headers.counter;
if(counnter<10000){
var calculateSum=calculatedSum(counter);
var answer="the sum is"+ calculateSum;
res.status(200).send(answer) // .status(200) is used
}
else{
res.status(411).send("Cannot solve large amount. Please Use Others server") // .status(411) is used
}
}
Response Body Type
Till now we were sending Text.
- JSON body
var answerObject={
Sum:calculatedSum
}
res.status(200).send(answerObject);
OR
var answerObject={
Sum:calculatedSum
}
res.status(200).json(answerObject);
- HTML body
res.send("<head><title>Hello from Page</title></head><body><i>hi there</i></body")
- Sending file
res.ssend(__dirname_ +'./index.html')
Server can talk to server
Example
step-1 Create index.js and run below code using node index.js
const express = require("express");
const app = express();
const port = 3000;
function calculateSum(counter) {
var sum = 0;
for (var i = 0; i <= counter; i++) {
sum = sum + i;
}
return sum;
}
function handleFirstRequest(req, res) {
console.log(req.query);
var lastIndex = req.query.counter;
console.log(lastIndex);
var calculatedSum = calculateSum(lastIndex);
var answer = "Calculated Sum is : " + calculatedSum;
console.log(answer);
res.send(answer);
}
app.get("/handlesum", handleFirstRequest);
app.listen(port, () => {
console.log(`Example app listening on port ${port}`);
});
Step-2 Create new file responseServer.js
and run below code using node responseServer.js
function logResponse(jsonBody) {
console.log(jsonBody);
}
function callbackFn(result) {
result.json().then(logResponse)
}
var sendObj = {
method: "GET"
};
fetch("http://localhost:3000/handlesum?counter=10", sendObj).then(callbackFn)