Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

express request and response processing #5188

Open
ganeshkbhat opened this issue May 7, 2023 · 2 comments
Open

express request and response processing #5188

ganeshkbhat opened this issue May 7, 2023 · 2 comments
Labels

Comments

@ganeshkbhat
Copy link

ganeshkbhat commented May 7, 2023

I see that expressjs app has a stack of Layer object Arrays. Which is the function that is used to pass the

I want to know:

  • When a request is sent from the http client which function is invoked first and how is the stack array functions with middlewares invoked and results passed stepwise?
  • Here is an example app.get(path, [middleware(req, res, next), m2(req, res, next), m3(req, res, next), m4(req, res, next)], handler(req, res)) => which function invokes functions stepwise => [middleware(req, res, next), m2(req, res, next), m3(req, res, next), m4(req, res, next), handler(req, res)]
  • Where is the request and response objects created that is passed as arguments?
  • Where is the response object callback function for send => onfinish/ onend functions which I can use to get the send/ sendfile's response object into a variable for an interceptor?

I am looking for the entry of the request and exit if the response (with the response result).

@Zimmad
Copy link

Zimmad commented May 30, 2023

When a request is sent from the HTTP client to an Express.js application, the initial function that is invoked is the one that handles the incoming request. In Express.js, this is typically referred to as a "route handler" or simply a "handler function". The handler function is responsible for processing the request and generating a response.

In the example you provided, the handler function is the one specified as the last parameter in the app.get method. It is the final function in the stack and is invoked after all the middlewares have been executed.

The stack of functions with middlewares is invoked stepwise in the order they are defined in the array. Each middleware function is invoked in sequence, passing the req (request) and res (response) objects along with the next function to the next middleware in the stack. The next function is used to pass control to the next middleware function in the stack.

The request and response objects are created and passed automatically by Express.js as part of the HTTP request-response cycle. When a request is received, Express.js creates the request object (req) and the response object (res) and passes them to the corresponding handler or middleware functions. These objects contain useful properties and methods that allow you to access information about the request and send the response back to the client.

Regarding the response object callback function for res.send, Express.js does not expose a specific callback function for send. Instead, the response is sent back to the client when you invoke res.send() or any other response-sending methods. If you need to perform additional operations or intercept the response before it is sent, you can create a custom middleware function that sits in the stack before the final handler function. This middleware can access the response object and modify its behavior as needed.

In summary, the entry point for the request is the handler function that handles the specific route, and the exit point for the response is the res.send or other response-sending methods. The request and response objects are automatically created by Express.js and are passed along the middleware stack and to the handler function. You can create custom middleware functions to intercept and modify the response before it is sent back to the client.

@ganeshkbhat
Copy link
Author

ganeshkbhat commented Jul 8, 2023

tried this and failed with errors. if I make an object using the express/request.js and express/response.js and want to traverse the express app router path with their middlewares to get the expected results from a non listening app like from a http server which function do i use and what options do i have?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants