Can Someone Please Explain Req In Express Plz
Solution 1:
HTTP is an application, client-server protocol. Everytime that a client want the server to perform an action, it has to make a request. The HTTP protocol defines a set of actions or verbs that are available to the client so it can make each request using one specific verb (GET, POST, PATCH, PUT, DELETE, etc). It doesn't matter what verb the client uses, only the client can initiate a comunication with the server using one of that verbs. So this is how exactly an HTTP GET request looks like:
GET/HTTP/1.1Host:example.comUser-Agent:curl/7.69.1Accept:*/*
The first line contains the verb used, in this case GET
, the path requested, in this case /
, and the protocol version, in this case HTTP/1.1
. The next lines, are a set of key value pairs called the headers
of that request, which can define a lot of aspects of the request made by the client to the server. By the way, an HTTP server never could or will start a request to a client, a client always is the one that make the request, and the server is always the one that response that request. One of the aspects of that request for example, is the destination host, that is present in the header host
with the value of example.com
. Bellow of the headers, all the HTTP requests have a blank line and then the body of the request, which normally contains the data that is sent from the client to the server. In this case, no data body is sent on the request.
Express is an HTTP server based on the HTTP module available on Node.js. Express simplifies the way that the native Node.js HTTP server works. Here is tipically how an Express app looks like:
const express = require('express');
const app = express();
// This is called a router
app.get('/path', (req, res) => {
// This router will perform some action with the req object// And will send a response to the client
});
So, on the example above, the method app.get(...)
, available on Express applications, allows the server to deal with the GET requests that come from the client. The app.get()
method takes two arguments, a path
and a callback function. The path
argument represent the string that goes after the name server, for example, in the URL www.example.com/test
, the hostname is www.example.com
, and the path is /test
. That method app.get()
is also called a Router
. So, the router of the example will deal with the GET
requests to that server which also define /path
value as the path the request is sent to. Once a request to the server fit those two conditions, that callback will be triggered.
So, finally we get to the answer. The res
variable is an object (a set of key-pair values separated by commas and locked into curly braces), that contains the data of the HTTP request, into a friendly legible object. For example, if you want to print to the console the path that the client used, you can print it like this console.log(req.path)
, or you can get all the headers of that HTTP request, you can use console.log(req.headers)
. The req
object is one of the 5 main objects in Express, in fact the Express documentation defines a ton of methods that you can use with the request object (req
). To get deep into the request object, you can see the official Express documentation in this link. The callback defined into the router, can use the req
object, to extract information of the client's request, process it and return a response to the client later.
Solution 2:
With an express server, you get two objects passed to a request handler.
req
is data about the incoming request (things that were sent from the client). It contains the headers on the request, it contains a parsed query string, it contains the URL path, it's generally the object where middleware puts things for request handlers to use. While Express adds a bit more to this object, you can see the general concept of the req
object by looking and the http.IncomingMessage
object documented here. This is what the object starts out as and then Express adds more to it. The express version of the object is documented here.
res
is the response object. This is all about sending a response. It will hold the outbound headers you want to send with the request. It contains the methods you use for sending a response. The core object is an http.ServerResponse
object documented here and then Express adds some more things to the object on top of that which is document here.
Post a Comment for "Can Someone Please Explain Req In Express Plz"