ROUTER
Body

Router - Body

The Router is responsible for matching routes with the correct structure, including routes that send data in the body. However, the Router itself does not handle parsing or converting the body content. For parsing and converting the body, you must use the parseData module.

The parseData module supports parsing various types of body data, including JSON, URL-encoded form data, and multipart form data (with file uploads). Without parseData, the body content would not be processed or made available automatically in the route handler.

Body Formats Supported by parseData

  • JSON: Parses the JSON body only if the request contains the Content-Type: application/json header. The JSON data is then converted into a JavaScript object accessible via req.body.
  • URL-encoded form data: Parses data sent with the Content-Type: application/x-www-form-urlencoded content type.
  • Multipart form data: Parses data sent with the Content-Type: multipart/form-data content type, including file uploads.

Example: JSON Body

In this example, parseData is used to parse a JSON body, but it will only work if the Content-Type is set to application/json:

Client Request (JSON):

const data = { firstName: "Mario", lastName: "Elvio" }
 
await fetch("http://domain/", {
  method: "POST",
  headers: {
    "Content-Type": "application/json"
  },
  body: JSON.stringify(data)
})

Router Example (JSON):

import v from "vkrun"
 
const vkrun = v.App()
vkrun.parseData()
 
vkrun.post("/", (req: v.Request, res: v.Response) => {
  const firstName = req.body.firstName
  const lastName = req.body.lastName
  res.status(200).end(`Hello, ${firstName} ${lastName}`)
})

Example: URL-encoded Form Data

In this example, parseData is used to parse URL-encoded form data, which is processed when the Content-Type is application/x-www-form-urlencoded:

Client Request (URL-encoded):

await fetch("http://domain/form", {
  method: "POST",
  headers: {
    "Content-Type": "application/x-www-form-urlencoded"
  },
  body: "firstName=Mario&lastName=Elvio"
})

Router Example (URL-encoded):

import v from "vkrun"
 
const vkrun = v.App()
vkrun.parseData()
 
vkrun.post("/form", (req: v.Request, res: v.Response) => {
  const firstName = req.body.firstName
  const lastName = req.body.lastName
  res.status(200).end(`Hello, ${firstName} ${lastName}`)
})

Example: Multipart Form Data (with File Uploads)

In this example, parseData is used to parse multipart form data, including handling file uploads, when the Content-Type is multipart/form-data.

Client Request (Multipart Form Data):

const formData = new FormData()
formData.append("username", "john_doe")
formData.append("file", fileInput.files[0])  // fileInput is a file input element
 
await fetch("http://domain/upload", {
  method: "POST",
  body: formData
})

Router Example (Multipart Form Data):

import v from "vkrun"
 
const vkrun = v.App()
vkrun.parseData()
 
vkrun.post("/upload", (req: v.Request, res: v.Response) => {
  const username = req.body.username
  const file = req.files[0] // Assuming a single file upload
  console.log(`Uploaded by ${username}: ${file.filename} (${file.mimetype})`)
  
  res.status(200).end("File uploaded successfully")
})
  • Note: req.files is an array of files, each containing properties such as filename, mimetype, size, and a buffer holding the file data.

Key Point

The Router only matches the route; it does not parse the body of requests. To handle body parsing, the parseData module is required. The module can process JSON (when the Content-Type is application/json), URL-encoded form data, and multipart form data (including file uploads).

see more about parse data: parseData documentation

Copyright © 2024 MIT by Mario Elvio