• 02-Jan-2023
Lightrun Team
Author Lightrun Team
Share
This is a glossary of all the common issues in Typestack - Routing Controllers

Troubleshooting Common Issues in Typestack – Routing Controllers

Lightrun Team
Lightrun Team
02-Jan-2023

Project Description

 

Routing Controllers is a library for creating expressive, elegant and scalable Node.js server-side APIs using TypeScript. It is part of the TypeStack suite of libraries, which are designed to provide a set of opinionated, modular tools for building robust and maintainable server-side applications using TypeScript.

With Routing Controllers, you can define your API routes and controllers using decorators and TypeScript classes, making it easy to build and maintain a scalable and expressive server-side API.

It supports features such as automatic route generation, request validation, and error handling, making it a powerful and flexible tool for building APIs with Node.js and TypeScript.

 

Troubleshooting Typestack – Routing Controllers with the Lightrun Developer Observability Platform

 

Getting a sense of what’s actually happening inside a live application is a frustrating experience, one that relies mostly on querying and observing whatever logs were written during development.
Lightrun is a Developer Observability Platform, allowing developers to add telemetry to live applications in real-time, on-demand, and right from the IDE.
  • Instantly add logs to, set metrics in, and take snapshots of live applications
  • Insights delivered straight to your IDE or CLI
  • Works where you do: dev, QA, staging, CI/CD, and production

The most common issues for Typestack – Routing Controllers are:

 

Question: how to get raw request body

 

An effective resolution to the issue can be reached by introducing specialized middleware.

// RawBodyMiddleware.ts
import { Request, Response } from 'express';

const RawBodyMiddleware = (req: Request, res: Response, next: () => void) => {
  const body = []
  req.on('data', chunk => {
    body.push(chunk)
  })
  req.on('end', () => {
    const rawBody = Buffer.concat(body)
    req['rawBody'] = rawBody
    switch (req.header('content-type')) {
      case 'application/json':
        req.body = JSON.parse(rawBody.toString())
        break
      // add more body parsing if needs be
      default:
    }
    next()
  })
  req.on('error', () => {
    res.sendStatus(400)
  })
}

export default RawBodyMiddleware

To ensure the endpoint requiring raw body access is handled properly, incorporate middleware within your controller and then utilize the @Req() decorator to obtain information from the request’s raw body property.

import { Request, Response } from 'express';
import { Req, UseBefore } from 'routing-controllers';
import RawBodyMiddleware from '../middlewares/RawBodyMiddleware'

@Post('/myendpoint')
@UseBefore(RawBodyMiddleware)
public myEndpoint(@Req() request: Request) {
   const rawBody = request.rawBody
   const asString = rawBody.toString()

   const jsonBody = request.body
}

 

Validate array in Body decorator

 

To validate an array in the body of a request using the Routing Controllers library, you can use the @Body decorator and the @IsArray decorator from the class-validator library.

Here’s an example of how you can use these decorators to validate an array in the body of a request:

import { Body, JsonController, Post } from 'routing-controllers';
import { IsArray } from 'class-validator';

class MyBody {
  @IsArray()
  items: string[];
}

@JsonController()
class MyController {
  @Post('/items')
  create(@Body() body: MyBody) {
    // body.items is guaranteed to be an array of strings at this point
  }
}

In this example, the @IsArray decorator is used to validate that the items field in the request body is an array. If the items field is not an array, the validation will fail and an error will be returned.

You can also use the @IsArray decorator to validate that the array contains elements of a specific type, by specifying the type as a generic argument to the decorator. For example:

import { Body, JsonController, Post } from 'routing-controllers';
import { IsArray } from 'class-validator';

class MyBody {
  @IsArray()
  items: number[];
}

@JsonController()
class MyController {
  @Post('/items')
  create(@Body() body: MyBody) {
    // body.items is guaranteed to be an array of numbers at this point
  }
}

This will validate that the items field is an array of numbers, and will fail the validation if any of the elements in the array are not numbers.

 

More issues from Typestack repos

 

Troubleshooting typestack-class-validator

Share

It’s Really not that Complicated.

You can actually understand what’s going on inside your live applications. It’s a registration form away.

Get Lightrun

Lets Talk!

Looking for more information about Lightrun and debugging?
We’d love to hear from you!
Drop us a line and we’ll get back to you shortly.

By submitting this form, I agree to Lightrun’s Privacy Policy and Terms of Use.