Skip to content

Instantly share code, notes, and snippets.

@mirzap
Forked from ninjascribble/node-user-agent.js
Created September 27, 2020 16:31
Show Gist options
  • Save mirzap/7f4a141bdaf892aa9a58e162700fb881 to your computer and use it in GitHub Desktop.
Save mirzap/7f4a141bdaf892aa9a58e162700fb881 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'));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment