Javascript 如何通过从另一个进程提供参数来执行文件

Javascript 如何通过从另一个进程提供参数来执行文件,javascript,node.js,adonis.js,Javascript,Node.js,Adonis.js,如何通过了解文件的位置和内容,从其他程序中提供一些参数来运行/执行节点文件的实例 当我从我的web应用程序(也在节点(adonis js)上运行)添加设备时,它会使用提供的参数创建一个设备,如设备名称、类型、lat、lng等。当设备成功添加时,添加设备时,以下代码的实例或副本应自动使用提供的参数启动 需要使用给定参数启动的文件 // c:/device/test.js var mqtt = require('mqtt'); function UserModule() { var clie

如何通过了解文件的位置和内容,从其他程序中提供一些参数来运行/执行节点文件的实例

当我从我的web应用程序(也在节点(adonis js)上运行)添加设备时,它会使用提供的参数创建一个设备,如设备名称、类型、lat、lng等。当设备成功添加时,添加设备时,以下代码的实例或副本应自动使用提供的参数启动

需要使用给定参数启动的文件

   // c:/device/test.js

var mqtt = require('mqtt');
function UserModule() {
var client  = mqtt.connect('mqtt://test.mosquitto.org');
client.on('connect', function () {
    let latitude = 777
    let lngitude = 999
setInterval(function() {
    let device_data = {
        'device_name':'{params.devcie_name}',
        'topic_topic':'MyProject/{params.type_of_device}/{params.user_id}/{params.topic_name}',
        'type':'GPS', 
        'data':{
            'lat':'35.'+latitude++,
            'lng':'-74.'+lngitude++,
            'alt':22,
            'acc':25.10101,
            'name': 'my car2',
            'type': 'car'
        },
        'status': 'OK'
    }
client.publish(device_data.topic_topic, JSON.stringify(device_data));
console.log('data published for', device_data.device_name);
}, 1000);
});
}
module.exports = UserModule;
添加设备的控制器

//app/controllers/http/devicecontroller.js

async store({ params, request, response, view, session, auth }) {
    try {
        const deviceData = request.only(['cat_id', 'device_name', 'type', 'device_type_id'])
        deviceData.device_owner_id = auth.current.user.id
        deviceData.is_deleted = 0
        deviceData.is_active = 1
        const device = new Device();

        let rules = Config.get('rules.device')
        let messages = Config.get('messages.device')

        const validation = await validate(deviceData, rules, messages)
        if (validation.fails()) {
            console.log(JSON.stringify(validation.messages()))
            session.flash({ type: 'danger', message: validation.messages()[0].message })
            return response.redirect('back')
        }

        let dev = await deviceService.addDevice(deviceData);
        session.flash({ type: 'success', message: 'Device added successfully' })
//here to run a code which execute that file
//sudo-code

File f  = new File('c:/device/test.js')
let content  = await f.readAll()

content = string.format(content, params1, params2, params3..)

f.content = content;
f.eecute()


        return response.redirect('/dashboard/device-manage')
    } catch (error) {
        session.flash({ type: 'danger', message: error.message })
        return response.redirect('back')
    }
}

每次添加设备时,每次使用新参数执行代码意味着使用新参数的新实例执行相同的文件。

要启动新进程,请使用节点包子进程

要向该子进程发送消息,请使用child\u process.fork

const cp = require('child_process');
const n = cp.fork(`${__dirname}/sub.js`);

n.on('message', (m) => {
  console.log('PARENT got message:', m);
});

// Causes the child to print: CHILD got message: { hello: 'world' }
n.send({ hello: 'world' });

是否要
将其作为一个新的单独流程执行?或者你只是想像调用同一进程中的函数一样调用它?一个单独的进程,可能是通过单击另一个按钮,我应该停止/终止它process@TKoL我还用sudo代码更新了这个问题,我需要按如下方式执行