Last active
September 30, 2024 22:28
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** 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')); | |
}); |
Thx, though my dad said it was req.Headers.get("user-agent")
.
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
Thanks!