I'm developing a node.js application and I'd like to know if this is possible and if so, how: After I start the server, the node.js should open a new window on the server side and show an image generated from the code.
-
with the right combination of software, almost anything is possible – Jaromanda X Oct 18 '15 at 08:29
-
@eko See open module. – user2226755 Oct 18 '15 at 08:43
3 Answers
Its not good approach to open any window on server side, as server is going to accessed by various users.
But if you want to do so, you can use child_process module of nodejs and execute that image with extension.
Example
var exec = require('child_process').exec,
child;
child = exec('<image with location & extension>',
function (error, stdout, stderr) {
console.log('Image opened');
if (error !== null) {
console.log('exec error: ' + error);
}
});
Hello world example with express.
var express = require('express');
var app = express();
app.get('/', function (req, res) {
var exec = require('child_process').exec,
child;
child = exec('a.jpg',
function (error, stdout, stderr) {
console.log('Image opened');
if (error !== null) {
console.log('exec error: ' + error);
}
});
res.send('Hello World!');
});
var server = app.listen(3000, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});

- 7,606
- 6
- 40
- 65
-
Perfect, thank you! Having multiple users isn't a problem, as I'm going to open oly one image that is visible when the server is running. – eko Oct 18 '15 at 08:43
You can use open to open in browser, with image url.
var open = require('open');
open('http://127.0.0.1/yourimage.png');
Or maybe use Node-Webkit rather nodejs.
Full code :
var fs = require('fs'),
http = require('http'),
url = require('url'),
open = require('open');
http.createServer(function(req, res) {
var request = url.parse(req.url, true);
var action = request.pathname;
if (action == '/yourimage.png') {
var img = fs.readFileSync('./yourimage.png');
res.writeHead(200, {
'Content-Type': 'image/png'
});
res.end(img, 'binary');
}
}).listen(8080, '127.0.0.1');
open('http://127.0.0.1/yourimage.png');
Links :
- How to use nodejs to open default browser and navigate to a specific URL
- How to serve an image using nodejs
Node-Webkit :
If you use node-webkit you can make a window (with html/css).

- 1
- 1

- 12,494
- 5
- 50
- 73
It's impossible from my experience to open a new window from the server-side (or even from your code in most cases). What I would do is use some javascript code to make the content on the page dynamic (for example, look at angularjs) and display the image on the same page, even if the image is generated later on the server side. Another option is to open a new tab from the client side code for that image.

- 1,284
- 1
- 10
- 17