简体   繁体   English

Node.js-运行外部进程

[英]Nodejs - running external process

Need to build a back-end service using node-js which does the following. 需要使用node-js构建后端服务,该服务将执行以下操作。

  1. Accepts file-upload from client (browser) 接受来自客户端(浏览器)的文件上传
  2. Save the file on disk and update DB ( mongodb ) with new filename and status as saved 将文件保存在磁盘上,并使用保存的新文件名和状态更新数据库( mongodb
  3. Starts an long running process to parse the file ( written in python) and get feedback ( progress, error, success) , update DB with status feedback 启动一个长时间运行的过程来解析文件(用python编写)并获取反馈(进度,错误,成功),并使用状态反馈更新数据库

How do we call # 3 from node.js - it could be system call like python parse_file.py filename 我们如何从node.js调用#3-它可能是系统调用,例如python parse_file.py filename

app.post('/upload',function(req,res, next){

       // what should go here to call the process

        res.writeHead(201, {'Content-type': 'text/plain'});
        res.end("File uploaded.");
    }
});

You can use require('child_process').spawn() or require('child_process').exec() to spawn a child process. 您可以使用require('child_process').spawn()require('child_process').exec()生成子进程。 The docs for those can be found at https://nodejs.org/dist/latest-v6.x/docs/api/child_process.html#child_process_child_process_spawn_command_args_options 这些文档可以在https://nodejs.org/dist/latest-v6.x/docs/api/child_process.html#child_process_child_process_spawn_command_args_options中找到

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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