简体   繁体   中英

Using Hapi.js run a third party command line tool

In my application I need to use a command line tool but I have not seen any way to do it without using a npm module. I am using core node except for the command line tool.

You can use node's child_process module. Here is an example where the touch command is called in the handler:

var ChildProcess = require('child_process');
var Hapi = require('hapi');

var server = new Hapi.Server();

server.route({
    method: 'GET',
    path: '/',
    handler: function (request, reply) {

        ChildProcess.exec('touch example.txt', function (err) {

            console.log('FILE CREATED');
        });

        process.on('exit', function (code) {

            console.log('PROCESS FINISHED');
            reply();
        });
    }
});

server.inject('/', function (res) { });

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