I need get screen resolution with node.js, but the following code don't work.
var w = screen.width;
var h = screen.height;
This also don't work.
var w = window.screen.width;
var h = window.screen.height;
Someone know how to get screen resolution with node.js ? Thank you.
I need get screen resolution with node.js, but the following code don't work.
var w = screen.width;
var h = screen.height;
This also don't work.
var w = window.screen.width;
var h = window.screen.height;
Someone know how to get screen resolution with node.js ? Thank you.
Share Improve this question edited Sep 24, 2013 at 18:39 Eugene Naydenov 7,2953 gold badges28 silver badges44 bronze badges asked Sep 24, 2013 at 18:28 AgneliAgneli 1551 gold badge1 silver badge7 bronze badges 5- What if the server isn't running a graphical interface, which is most likely the case? – Joe Doyle Commented Sep 24, 2013 at 19:37
- 1 Why would it be a server? – Jean-Philippe Leclerc Commented Sep 24, 2013 at 20:37
- 4 Because Node.js doesn't run client side, it runs on the server. – hexacyanide Commented Sep 24, 2013 at 22:46
- Joe, in this case i'm sure that user have a GUI – Agneli Commented Sep 25, 2013 at 0:27
- 4 "Nodejs doesn't run on the client side" - is silly an untrue. That's like saying "Java doesn't run on the client side". ElectronJs and a host of other technologies all run nodejs on workstations. – Arbiter Commented Mar 1, 2019 at 16:31
5 Answers
Reset to default 5There is now a screenres module via npm:
https://www.npmjs./package/screenres
You should retrieve the window.screen.width
and window.screen.height
in your client-side JavaScript code and send that information to your server-side Node.js application via AJAX, WebSocket, as form data, etc.
For example:
(function(w) {
$.ajax({
type: 'POST',
url: '/echo/json',
data: {
w: w.screen.width,
h: w.screen.height
},
success: function() {
console.log(arguments);
},
dataType: 'json'
});
})(window);
This is mon and is not related to the server-side technology. Doesn't matter what you use at the back-end (Node.js, Python, Ruby, PHP, Java, ASP.NET, etc.), you should send a data from client to server since the server doesn't deal with the user's browser directly.
I solved the problem with phantomJS
.
Install phantomJS
:
sudo apt-get install phantomjs
Create app.js file:
var w = screen.width;
var h = screen.height;
console.log(w+"x"+h);
phantom.exit();
Execute:
phantomjs app.js
Return:
1366x768
This is quite an overkill, but the node module I found doesn't work anymore and it doesn't handle multiple displays anyway:
npm install nightmare
And then if you want for exampnle the size and coordinates of the external monitor if available:
const getBounds = async () => {
const nm = new Nightmare({
webPreferences: {
nodeIntegration: true,
},
});
const bounds = nm
.goto('about:blank')
.evaluate(() => {
const electron = require('electron');
const displays = electron.screen.getAllDisplays()
const display = displays.find((d) => d.bounds.x !== 0 || d.bounds.y !== 0) || displays[0];
return display.bounds;
}).end();
return bounds;
};
Using screenz:
const { width, height } = require("screenz");
console.log(width);
//=> 1920
console.log(height);
//=> 1080