mardi 21 août 2018

How to call node.js server javascript from html page

Situation

I have a html page which calls multiple javascript files. Everything works on client-side right now.

Because I need to execute a jar within javascript, I am switching to Node.js. (applets are deprecated)

However, I am new to node.js and confused about how to link everything.


I have :

index.html which calls various .js scripts (files.js,objects.js,etc.)

webServer.js which makes the node.js server

var http = require('http');
var fs = require('fs');
http.createServer(function (req, res) {
    fs.readFile('index.html', function(err, data) {
    res.writeHead(200, {'Content-Type': 'text/html'});
    res.write(data);
    res.end();
    });

}).listen(8080);

javaApp.js which executes a jar

var exec = require('child_process').exec;
var child = exec('java -jar E:/JavaApp.jar',
function (error, stdout, stderr){
    console.log('Output -> ' + stdout);
    if(error !== null){
        console.log("Error -> "+error);
    }
});
module.exports = child;


The question

I would like, when clicking on a button in my html, to call javaApp.js on the server side.

I know Express can be used to link index.html to webServer.js, but I don't understand how to link index.html to the code used by the server.

i.e. How can I call javaApp.js from index.html if there's no function name in it?

Is this answer relevant ? How to call node.js server side method from javascript?




Aucun commentaire:

Enregistrer un commentaire