THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.
In this tutorial, We will look into how to get Full URL in NestJS controller with code examples.
To get Full Url in NestJS controller, Please follow below steps
import {Request} from 'express';
Request
object into Controller method with type @Req
decorator @Get("url")
getUrl(@Req() req: Request): void {}
http
or https
protocol of calling URL const protocol = req.protocol;
const host = req.get('Host');
const originUrl = req.originalUrl;
const fullUrl = protocol + host + originUrl;
Here is an complete code example
import { Controller, Get, Req } from '@nestjs/common';
import { Request } from 'express';
@Controller("api")
export class AppController {
constructor() { }
@Get("url")
getUrl(@Req() req: Request): void {
const protocol = req.protocol;
const host = req.get('Host');
const originUrl = req.originalUrl;
const fullUrl = protocol + host + originUrl;
console.log(fullUrl)
}
}
It prints to console
http://localhost:3000/api/url
🧮 Tags
Recent posts
Nodejs package.json resolutions How to find Operating System username in NodeJS? How to convert Double to Integer or Integer to double in Dart| Flutter By Example Ways to skip test case execution in Gradle project build Learn Gradle | tutorials, and examplesRelated posts