Is it possible to run a Node script from a web page?

梦想的初衷 提交于 2021-02-07 03:24:36

问题


I'am searching for days now but could not get an answer. I would like to do the following:

  1. User connects to editor.html (Apache2 with basic http auth)
  2. User want to open a file (lets say /home/user1/myfile.txt) on the server with his user/pass (same as in passwd)
  3. Node.js Script gets startet with user rights from above and user can edit file

The Node Script will handle the connection via websockets and read/writes files.

I think the biggest problem is that its not possible to run a node script on the server from a web page... and I don´t want to involve any php/cgi scripts... only Apache and Node.js / JS.

Please also comment or answer if you know that it is really not possible...

Thanks!

Kodak

Edit: The workflow should be the following: User access webpage -> enters his credential (same as in passwd) -> node.js script gets started with the user rights of the logged in user -> files getting read or written with user rights

Biggest Problem: who starts the Node.js script? Apache? How?


回答1:


I hate to be this person, but...

That is not the way node is designed, it is designed to use the event loop, I would recommend having node serve the static files, maybe using apache as a proxy, then when someone requests a certain page, doing what ever needs to be done, if you really must spawn a child process, use child_process.spawn, as for the rights of the user, I recommend just passing in a code, like 1=admin, 2=user, 3=guest, and the child process can do what is needs.




回答2:


Use Socket.io - Official Socket.IO Website

You can also use Express with socket IO to create a separate app server. - Express JS Website

You may want to consider security implications of allowing a user to connect directly using their server side account. There are also many applications available that already do this that you might consider implementing instead of writing your own, with all the properly embedded security that will be required.




回答3:


Let your users GET static auth.html page (via apache) without any authentication.

Let form submit action is some auth.js (Node.js script). This auth.js check if user's authentication is success. If so it starts node.js server, setups socket.io on it and redirects user to some editor.html.

In this case as you can notice that there is an authentication based on node.js scripting. If you want basic apache2 one I can recommend you the next scenario:

There is auth.html and editor.html pages on the server. Last one placed in /private folder and direct access to this folder is denied by .htaccess. So when the user pass apache2 authentication in auth.html he GET this auth.html which is empty document with onload event handler that send AJAX to auth.js (Node.js script). Node.js get private/editor.html and send it to user like /editor.html.

In this case user never has an access to editor without passing authentication. And after authentication node.js server is started and socket.io is setup and everything fine.




回答4:


I found a solution:

It is possible to write a custom authentication program for apache with mod-auth-external: https://code.google.com/p/mod-auth-external/

With basic authentication enabled the webserver would pass the credentials to a script/program and this can then run the node app.



来源:https://stackoverflow.com/questions/19687604/is-it-possible-to-run-a-node-script-from-a-web-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!