0

I have an Express js server endpoint with the following directory structure:

  • app.js
  • node_modules
  • package-lock.json
  • package.json
  • mrz-detection

The mrz-detection directory, contains a node app which I would normally call from the command line like this node run/getMrz.js --file passport.jpg

My app.js looks like this

const { getMrz } = require('./mrz-detection');
const fs = require('fs');
const app = express()
const port = 3000
const bodyParser = require('body-parser');
app.use(bodyParser.json({limit: '10mb', extended: true}));
var passportsArr = [];


app.post('/', function(req, res) {
    var passport = req.body;
    let buff = new Buffer(passport.data, 'base64');
    fs.writeFileSync('passport.png', buff);
    // send passport photo to mrz-detection app here
})

app.listen(port, () => console.log(`Mrz detection app listening at http://localhost:${port}`))

I need to call the mrz-detection app from my post route in the app.js

How would I do that?

2 Answers2

0

There are two ways I can think of to do this:

  1. Call the other program command-line-style as explained by a commentor
  2. Refactor your code so that you can simply import a function from your other file and use it. I would recommend this more because it allows for complex parameters to potentially be passed without having to serialize and deserialize them in the command line.
Robert Moore
  • 2,207
  • 4
  • 22
  • 41
0

You could you start the app as a child process:

https://nodejs.org/api/child_process.html

There are a few different 'child_process' functions for spawning child processes

execFile spawn fork

With fork, sending messages between parent and child works automatically

const fork = require('child_process').fork;
const child = fork('/path/to/app.js', ['optional', 'arguments']);

// message from child
child.on('message', (msg) => {
    console.log('message from child ' + msg);
}

// send message to child
child.send({hi:'hi from parent'});
Melvin Sovereign
  • 455
  • 5
  • 10