I have written this example server,
runServer = function(){
var http = require("http");
http.createServer(function (request, response) {
request.on("end",
function ()
{
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('Hello HTTP!');
});
}).listen(8075);
}
now I want to run this server onClick from a html page, is it possible? If yes then how??
You would have to serve the HTML page on the same machine using a different web server (Such as the LAMP stack) and the use server side scripting to run the OS command "node servername.js"
Normally you need to save this code to the file and run from the command line:
node mycode.js
However if you want to start it from JavaScript on your local machine, there are also two solutions discussed here: a cross platform signed Java Applet and Windows specific WScript.Shell. Java applet could check the operating system and choose between Windows, Mac and Linux executables.
You may also produce a custom .exe file that would start node.exe also passing the required parameter to it.
However there are so many security problems with downloaded code from the web that regardless of solution the browser will likely show various scary warnings. It may not be so bad to consider starting the node executable bypassing the browser.
Sure, you could do that, but it doesn't really make much sense. If you're getting served an HTML page from a sever then why are you starting another server to serve http requests on another port? Why not just run your server and listen on both ports? What you probably just want to do is start your server from the command line by running
node myserver.js
in your terminal.
However, if for some reason you really want to start another http server from like a php script like that, what you can do is the following:
However, I think you really should do some reading up on the purpose of a node.js and how it acts as a web server before continuing, as your question seems to reflect a lack of understanding in that area.