Careeroza
Real World Example
Order Tracking Endpoint
Delivery apps like Swiggy, Zepto and Blinkit expose an order tracking endpoint that the app polls every few seconds. A NestJS version with a fixed route before the dynamic one, a query flag and caching headers:
src/orders/orders.controller.ts
import { Controller, Get, Header, Param, ParseBoolPipe, DefaultValuePipe, Query } from '@nestjs/common';
import { OrdersService } from './orders.service';
@Controller('orders')
export class OrdersController {
constructor(private readonly ordersService: OrdersService) {}
@Get('active') // fixed route first
active() {
return this.ordersService.findActiveForCurrentUser();
}
@Get(':orderId/track')
@Header('Cache-Control', 'no-store') // always fresh
track(
@Param('orderId') orderId: string,
@Query('withRider', new DefaultValuePipe(false), ParseBoolPipe) withRider: boolean,
) {
return this.ordersService.track(orderId, withRider);
}
}Example
curl "http://localhost:3000/orders/ORD-7781/track?withRider=true"Example output
HTTP/1.1 200 OK
Cache-Control: no-store
Content-Type: application/json; charset=utf-8
{"orderId":"ORD-7781","status":"OUT_FOR_DELIVERY","etaMinutes":12,"rider":{"name":"Ravi","phone":"+91-98XXXXXX21","lat":12.9716,"lng":77.5946}}