Troubleshooting Common Issues in Typestack – Routing Controllers
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
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
It’s Really not that Complicated.
You can actually understand what’s going on inside your live applications.