ROUTER
Methods

Router - Methods

A Router method corresponds to an HTTP method and is attached to an instance of the VkrunJS router.

MethodDescription
getHandles GET requests. Used to retrieve data.
postHandles POST requests. Used to create new resources.
putHandles PUT requests. Used to replace existing resources.
patchHandles PATCH requests. Used to partially modify a resource.
deleteHandles DELETE requests. Used to delete resources.
optionsHandles OPTIONS requests. Used to retrieve communication options.
headHandles HEAD requests. Used to retrieve only the headers without the body.

Example:

import v from "vkrun"
 
const vkrun = v.App()
const router = v.Router()
 
router.get("/", (req: v.Request, res: v.Response) => {
  res.status(200).send("GET method route")
})
 
router.post("/", (req: v.Request, res: v.Response) => {
  res.status(200).send("POST method route")
})
 
router.put("/", (req: v.Request, res: v.Response) => {
  res.status(200).send("PUT method route")
})
 
router.patch("/", (req: v.Request, res: v.Response) => {
  res.status(200).send("PATCH method route")
})
 
router.delete("/", (req: v.Request, res: v.Response) => {
  res.status(200).send("DELETE method route")
})
 
router.options("/", (req: v.Request, res: v.Response) => {
  res.status(200).end("OPTIONS method route")
})
 
router.head("/", (req: v.Request, res: v.Response) => {
  res.status(200).end() // The HEAD method does not send data in the response body
})
 
vkrun.use(router) // Inject all created routes
 
vkrun.server().listen(3000, () => {
  console.log("VkrunJS started on port 3000")
})

Equivalent:

import v from "vkrun"
 
const vkrun = v.App()
 
vkrun.get("/", (req: v.Request, res: v.Response) => {
  res.status(200).send("GET method route")
})
 
vkrun.post("/", (req: v.Request, res: v.Response) => {
  res.status(200).send("POST method route")
})
 
vkrun.put("/", (req: v.Request, res: v.Response) => {
  res.status(200).send("PUT method route")
})
 
vkrun.patch("/", (req: v.Request, res: v.Response) => {
  res.status(200).send("PATCH method route")
})
 
vkrun.delete("/", (req: v.Request, res: v.Response) => {
  res.status(200).send("DELETE method route")
})
 
vkrun.options("/", (req: v.Request, res: v.Response) => {
  res.status(200).end("OPTIONS method route")
})
 
vkrun.head("/", (req: v.Request, res: v.Response) => {
  res.status(200).end() // The HEAD method does not send data in the response body
})
 
vkrun.server().listen(3000, () => {
  console.log("VkrunJS started on port 3000")
})
Copyright © 2024 MIT by Mario Elvio