简体   繁体   中英

Nodejs Backend communicate to VueJS Frontend

I have on one side my VueJS app build with Webpack and on the server side I have Node.js. I followed this article to setup my client/server : https://medium.com/@anaida07/mevn-stack-application-part-1-3a27b61dcae0

On the server side, I open a .jar file with some arguments. Like that :

var exec = require('child_process').exec, child;
child = exec('/usr/bin/java -jar ./myjar.jar -arg -arg2',
  function (error, stdout, stderr){
    console.log('stdout: ' + stdout);
    console.log('stderr: ' + stderr);
    if(error !== null){
      console.log('exec error: ' + error);
    }
});

Everything works fine separately. But now, how to communicate between those two ? I want to do 2 things at the end : 1. Fetch the file output and display it into a VueJS component 2. Set arguments value using a form in my frontend.

How should i processed to achieve those two things ? Thanks for your time !

First : This can achieved easily using a http server Read https://nodejs.org/api/http.html

const http = require('http');
const exec = require('child_process').exec;
const server = http.createServer((req, res) => {
     child = exec(`/usr/bin/java -jar ./myjar.jar -arg -arg2`,function (error, stdout, stderr){
          res.write(stdout);
          console.log('stderr: ' + stderr);
if(error !== null){
  console.log('exec error: ' + error);
}
server.listen(8080);

Second , you can easily get the request parameters in the req object .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM