Is it possible to configure the user-agent of a simple Node JS server? For example, I would like to run my node server using an iPhone user agent to simulate the device display. Not sure if there is an NPM package doing this or a custom javascript to manipulate the user-agent of a Node JS server.
Detail: I am aware about express-user-agent
It will only give your parsing feature and access to your current user-agent within your Express app.
Here is the code of my Node JS server:
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io')(server);
var exec = require('child_process').exec;
var shell = require('shelljs');
app.use('/public', express.static(__dirname + '/public'));
app.get('/', function(req, res,next) {
res.sendFile(__dirname + '/index.html');
});
server.listen(4200);
io.on('connection', function(client) {
client.on('join', function(data) {
console.log(data);
});
client.on('command', function(data) {
console.log(data);
});
});
The Agent manages connection persistence for HTTP clients. It maintains a queue of pending requests for a given host and port, reusing a single socket connection for each until the queue is empty. After that, the socket is destroyed, if the keepAlive is set to false .
In simple words, it's a string of text that is unique for each software or browser on the internet and holds the technical information about your device and operating system. User-agent is present in the HTTP headers when the browser wants to connect with the webserver.
Look this:
var request = require('request');
var options = {
url: 'http://localhost:4200/test',
headers: {
'User-Agent': 'MY IPHINE 7s'
}
};
function callback(error, response, body) {
//do somethings
}
request(options, callback);
For example you can add test-route to your server and send get request. Dont forget about url to request, it must be http://localhost:4200/test, code:
...
app.use('/public', express.static(__dirname + '/public'));
app.get('/', function(req, res,next) {
res.sendFile(__dirname + '/index.html');
});
app.get('/test', function(req, res,next) {
console.log(req.headers);
});
server.listen(4200);
...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With