Changeset 36329


Ignore:
Timestamp:
2022-07-28T23:47:09+12:00 (2 weeks ago)
Author:
davidb
Message:

Simple Python script exec/spawn test added

File:
1 edited

Legend:

Unmodified
Added
Removed
  • other-projects/bib-stinky/trunk/nodejs-server/express-web-server.js

    r36315 r36329  
    11// Setup Express
    2 const path    = require('path')
     2const path      = require('path');
     3const { spawn } = require('child_process');
     4
    35const express = require("express");
    46const cors    = require("cors");
     7
    58
    69const app = express();
     
    2528
    2629
     30app.get("/python-test", (req, res) => {
     31
     32    var dataToSend;
     33
     34    // Spawn new child process to call the python script
     35    const python = spawn('python3', ['script1.py']);
     36   
     37    // Collect data from script
     38    python.stdout.on('data', function (data) {
     39    console.log('Pipe data from python script ...');
     40    dataToSend = data.toString();
     41    });
     42   
     43    // In close event we are sure that stream from child process is closed
     44    python.on('close', (code) => {
     45    console.log(`child process close all stdio with code ${code}`);
     46
     47    res.send(dataToSend)   
     48    });   
     49});
     50
     51
    2752app.listen(port, () => {
    2853  console.log(`Started CORS-enabled Express web server running on port ${port}.`);
Note: See TracChangeset for help on using the changeset viewer.