Skip to content

Instantly share code, notes, and snippets.

@ninjascribble
Last active September 30, 2024 22:28
Show Gist options
  • Save ninjascribble/5119003 to your computer and use it in GitHub Desktop.
Save ninjascribble/5119003 to your computer and use it in GitHub Desktop.
Fetching the user-agent string from a request using either NodeJS or NodeJS + Express
/** Native NodeJS */
var http = require('http')
, server = http.createServer(function(req) {
console.log(req.headers['user-agent']);
});
server.listen(3000, 'localhost');
/** NodeJS with Express */
var express = require('express')
, http = require('http')
, app = express();
http.createServer(app).listen(3000);
app.get('/', function(req, res) {
console.log(req.get('user-agent'));
});
@ali80
Copy link

ali80 commented Sep 24, 2021

thanks man

@mohammadiyan
Copy link

Thanks

@kasir-barati
Copy link

NestJS way:

import {
    Body,
    Controller,
    Headers,
    Post,
    Request,
    UseGuards,
} from '@nestjs/common';
import { ApiTags } from '@nestjs/swagger';
import { Request as ExpressRequest } from 'express';

import { AuthService } from './auth.service';
import {
    LoginDto,
    GeneratedJwtTokenResponseDto,
} from './dto';
import { LocalAuthGuard } from './local-auth.guard';

@ApiTags('Authentication')
@Controller('auth')
export class AuthController {
    constructor(private readonly authService: AuthService) {}

    @UseGuards(LocalAuthGuard)
    @Post('login')
    async login(
        @Request() req: ExpressRequest,
        @Body() requestBody: LoginDto,
        @Headers('User-Agent') userAgent: string, 
        // @Headers() headers: Record<string, any>, equivalent to req.headers. You can read by: headers['User-Agent']
    ): Promise<GeneratedJwtTokenResponseDto> {
        return await this.authService.login({
            user: req.user as any,
            ip: req.ip,
            userAgent,
        });
    }
}

@Lobaton2020
Copy link

Thanks guys

@dulanjanabandara
Copy link

Thanks guys!

@vladimir-acc
Copy link

vladimir-acc commented Aug 29, 2024

import {
    Body,
    Controller,
    Headers,
    Ip,
    Post,
    Request,
    UseGuards,
} from '@nestjs/common';
export class AuthController {
    constructor(private readonly authService: AuthService) {}

    @UseGuards(LocalAuthGuard)
    @Post('login')
    async login(
        @Request() req: ExpressRequest,
        @Body() requestBody: LoginDto,
        @Headers('User-Agent') userAgent: string, 
        @Ip() ip: string,
    ): Promise<GeneratedJwtTokenResponseDto> {
        return await this.authService.login({
            user: req.user as any,
            ip,
            userAgent,
        });
    }
}

@pete-crzy-coder
Copy link

Thanks!

@SilvanZwick
Copy link

Thx, though my dad said it was req.Headers.get("user-agent").

@SilvanZwick
Copy link

Thx, though my dad said it was req.Headers.get("user-agent").

Your way works though! (I tried yours first)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment