gpt4 book ai didi

javascript - Nodejs - 运行外部进程

转载 作者:行者123 更新时间:2023-12-03 06:42:55 24 4
gpt4 key购买 nike

需要使用 Node-js 构建一个后端服务,该服务执行以下操作。

  1. 接受来自客户端(浏览器)的文件上传
  2. 将文件保存在磁盘上,并使用新文件名和保存状态更新数据库 (mongodb)
  3. 启动一个长时间运行的进程来解析文件(用 python 编写)并获取反馈(进度、错误、成功),并使用状态反馈更新数据库

我们如何从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.");
}
});

最佳答案

您可以使用 require('child_process').spawn()require('child_process').exec() 生成子进程。这些文档可以在 https://nodejs.org/dist/latest-v6.x/docs/api/child_process.html#child_process_child_process_spawn_command_args_options 找到。

关于javascript - Nodejs - 运行外部进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37872867/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com