1

I'm trying to find out whether it's possible to invoke node.js functions from a web page. Is there any way to make node.js functions accessible from Google Chrome (so that they are run on the node.js server), as shown here?

(I'm aware that it's possible to do this using node-webkit (a non-standard Chromium implementation) without modifying the code, but I'd prefer to do this using an unmodified browser, which will require the code shown below to be modified in some way.)

<html>
    <body>
        <script type = "text/javascript">
            var exec = require('child_process').exec; //node.js function
        </script>
        <p onclick = "exec('firefox')">
            Click here to launch the Firefox web browser.
        </p>
   </body>
</html>
Anderson Green
  • 30,230
  • 67
  • 195
  • 328
  • This post discusses sharing scripts between the server and the client (which is what I'm trying to do.) http://stackoverflow.com/questions/4371196/node-js-and-client-sharing-the-same-scripts – Anderson Green Nov 18 '12 at 06:00

2 Answers2

2

No, this is not possible, for clear security reasons.

You only have available to you what the browser gives you. node-webkit is the closest thing available, and does not meet your requirements.

Brad
  • 159,648
  • 54
  • 349
  • 530
  • I know of a node.js library called nowjs that allows node.js functions to be invoked from the browser - would it be possible to implement some kind of workaround using nowjs? – Anderson Green Nov 18 '12 at 05:44
  • Nowjs requires a server. If you don't want to run anything outside of the browser, then this is not an option for you. Of course if installing things on the client is fair game, then there are hundreds of possibilities. – Brad Nov 18 '12 at 05:45
  • I actually want to mimic the features of node-webkit using a client *and* a server - is this possible? – Anderson Green Nov 18 '12 at 05:49
  • @AndersonGreen, Yes, it is possible but the things that will execute will happen on the server. It seems that you wanted to execute them on the client. Can you clarify what you are trying to do? You can definitely send commands from the client to the server to execute. – Brad Nov 18 '12 at 06:01
  • I'm basically trying to share entire scripts between the client and the server (in order to mimic the features of node-webkit as closely as possible.) – Anderson Green Nov 18 '12 at 06:03
  • @AndersonGreen, No problem. What features, specifically, are you trying to use? Where should these features run? In your example, you execute another program. Again, the only way to do this is to execute that on the server. If the server and client are the same machine, then there is no problem. However, I assume if you don't want to install anything on the client, that won't be the case. – Brad Nov 18 '12 at 06:05
  • The client and the server are on the same machine in this case. – Anderson Green Nov 18 '12 at 06:06
  • 1
    @AndersonGreen, In that case, use something like Socket.IO to emit code to `exec()` on the server end. I just build a project recently that does the reverse of this, with decent success. It isn't difficult for the basics. When you get into needing to require scripts and what not, it may get more complicated, due to scoping and what not. – Brad Nov 18 '12 at 06:08
  • 1
    I am using now.js and it works wonderfully well. The server can be on the same machine as the client or elsewhere. I'm doing all development with the server on the same pc as the client. That said now.js is no longer under development. However it works fine and their are alternatives. – nevf Nov 18 '12 at 07:48
  • I think this answer should be modified in order to reflect the changes that have been made to the original question - this answer seems slightly inaccurate in its present form. – Anderson Green Nov 25 '12 at 19:56
  • Technically, with a question phrased as client/server, yes. You then make a socket call, as mentioned above, or an even simpler POST/GET request to server-side file that will execute the code. Obviously, this is useful only when you are running your files as localhost. With nodejs + express, no more is required than specify a route. app.get('/exec/:id', function(req, res) { var fs = require('fs'); /* do something on the file system */}); – widged Mar 08 '13 at 01:21
2

NW has own method like node exec :

var gui = require('nw.gui');
gui.Shell.openItem('firefox', ,function(error, stdout, stderr) { });
oknoorap
  • 1,923
  • 2
  • 14
  • 20
  • Does NW stand for "node-webkit"? – Anderson Green Nov 20 '12 at 05:43
  • yes node-webkit :), it will open shell external program, further information the wiki pages : https://github.com/rogerwang/node-webkit/wiki/_pages https://github.com/rogerwang/node-webkit/wiki/Shell – oknoorap Nov 21 '12 at 17:16
  • Also, how does this answer my question specifically (mimicking the features of node-webkit using a Google Chrome browser client and a node.js server?) – Anderson Green Nov 21 '12 at 17:19
  • 1
    I think node-webkit is client-side only, if you want bridge between server-side and client-side, appjs is wonderful. – oknoorap Nov 23 '12 at 01:55
  • Is this answer relevant to appjs in any way? I still don't see how it answers the question. – Anderson Green Nov 23 '12 at 16:49