ROUTER
Handlers
Request

Router - Request

VkrunJS utilizes the http.IncomingMessage from Node.js' createServer to handle requests. The IncomingMessage object provides methods and properties to access the data sent by the client in an HTTP request. For more details on the native IncomingMessage, refer to the Node.js IncomingMessage documentation (opens in a new tab).

Note: It's important to note that the version of IncomingMessage used in VkrunJS depends on the Node.js version installed in your environment. Therefore, there might be slight variations in functionality between different Node.js versions.

Request Properties

Some examples of native properties provided by http.IncomingMessage:

PropertyDescription
req.methodThe HTTP method used for the request (GET, POST, PUT, etc.).
req.urlThe full URL of the request.
req.headersAn object containing request headers.

Node.js IncomingMessage documentation (opens in a new tab).

Example: Basic Request Object

import v from "vkrun"
 
const vkrun = v.App()
 
vkrun.get("/example", (req: v.Request, res: v.Response) => {
  console.log(req.method)  // Outputs: GET
  console.log(req.url)      // Outputs: /example?name=John
  console.log(req.headers)  // Outputs: { host: "localhost:3000", ... }
  res.status(200).end("Request received")
})

In addition to the native properties provided by IncomingMessage, VkrunJS allows for parsing query strings, URL parameters, and body data through the parseData middleware. This middleware adds req.query, req.params, and req.body to the request object.

PropertyDescription
req.paramsParameters parsed from the URL (e.g., /:id in /users/:id).
req.queryAn object containing query parameters (e.g., ?name=John).
req.bodyThe body data of the request (for POST, PUT and PATCH requests).

Key Points

  • The request object in VkrunJS is based on Node.js' http.IncomingMessage.
  • You can access request data such as URL parameters, query strings, and headers directly when using parseData.
  • Body data is not parsed automatically; use the parseData middleware for handling body content.
Copyright © 2024 MIT by Mario Elvio