如何一步步同步执行后端(Node.js Express)中的命令?
How to execute commands in backend(Node.js Express) synchronously step by step?
我想做这个。但是我无法同步 运行 所有这些。每一步都在同时执行。有人可以帮忙吗?
我需要在 Node.js express 中完成这个后端任务。
第一步:从前端输入关键字(react中写的)
第 2 步:运行 命令:“shell.exec(__dirname + /run.sh "${data}"
);”在后端。 [[data是从前端接收到的关键字]]。它将在我的文件中搜索关键字并生成一个 csv 文件。
第 3 步:运行 一个 python 脚本,它将在第 2 步执行完成后执行 python 文件 (csv_to_html.py)。它将生成的 csv 文件转换为 html table 并创建一个 output.html 文件
第 4 步:完成第 3 步后,将生成一个 output.html 文件。在搜索框下方的前屏幕(index.html 或 index.ejs 或 index.hbs)的 iframe 中发送此文件。欢迎就如何在前端显示它提出建议。
所有这些都应该动态完成。
这是我所做的:
const { response } = require("express");
const express = require("express");
const bodyParser = require("body-parser");
const shell = require("shelljs");
const app = express();
const spawn = require("child_process").spawn;
app.use(express.static("public"));
app.use(bodyParser.urlencoded({ extended: true }));
var data = "";
app.get("/", function (req, res) {
shell.exec(__dirname + `/run.sh "${data}"`);
const pythonProcess = spawn('python', ["csv_to_html.py"]);
res.sendFile(__dirname + "/index.html");
});
app.post("/", function (req, res) {
data = req.body.load_data;
res.redirect("/"));
});
app.listen(3000, function () {
console.log("Starting server at port 3000...");
});
- 启用 json 正文数据解析。
- 将脚本的执行移至我们的 POST 请求,以便它们仅在 运行 将新数据发送到服务器时才执行。
- 删除
shelljs
并仅依靠 Node.JS 自己的 child_process 来调用命令。
- 嵌套我们的回调,以便它们按所需顺序执行。
按照这些步骤,您最终会得到以下文件(在修改的行中添加了数字的注释):
const express = require("express");
const bodyParser = require("body-parser");
const app = express();
// Modification #3
const { exec } = require("child_process");
app.use(express.static("public"));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json()); // Modification #1
app.get("/", function (req, res) {
res.sendFile(__dirname + "/index.html");
});
app.post("/", async function (req, res) {
// Modification #2
const data = req.body.load_data;
// Modification #3 & #4
exec(`./run.sh "${data}"`, async (err, stdout, stderr) => {
if (err) console.error(err)
console.log(stdout);
exec('python csv_to_html.py', (err, stdout, stderr) => {
console.log(stdout);
res.redirect("/");
});
});
});
app.listen(3000, function () {
console.log("Starting server at port 3000...");
});
发送到 POST 路由的请求正文应为 Content-Type application/json
,并且类似于以下内容:
{
"load_data": "argumentForRunSH"
}
我想做这个。但是我无法同步 运行 所有这些。每一步都在同时执行。有人可以帮忙吗?
我需要在 Node.js express 中完成这个后端任务。
第一步:从前端输入关键字(react中写的)
第 2 步:运行 命令:“shell.exec(__dirname + /run.sh "${data}"
);”在后端。 [[data是从前端接收到的关键字]]。它将在我的文件中搜索关键字并生成一个 csv 文件。
第 3 步:运行 一个 python 脚本,它将在第 2 步执行完成后执行 python 文件 (csv_to_html.py)。它将生成的 csv 文件转换为 html table 并创建一个 output.html 文件
第 4 步:完成第 3 步后,将生成一个 output.html 文件。在搜索框下方的前屏幕(index.html 或 index.ejs 或 index.hbs)的 iframe 中发送此文件。欢迎就如何在前端显示它提出建议。
所有这些都应该动态完成。
这是我所做的:
const { response } = require("express");
const express = require("express");
const bodyParser = require("body-parser");
const shell = require("shelljs");
const app = express();
const spawn = require("child_process").spawn;
app.use(express.static("public"));
app.use(bodyParser.urlencoded({ extended: true }));
var data = "";
app.get("/", function (req, res) {
shell.exec(__dirname + `/run.sh "${data}"`);
const pythonProcess = spawn('python', ["csv_to_html.py"]);
res.sendFile(__dirname + "/index.html");
});
app.post("/", function (req, res) {
data = req.body.load_data;
res.redirect("/"));
});
app.listen(3000, function () {
console.log("Starting server at port 3000...");
});
- 启用 json 正文数据解析。
- 将脚本的执行移至我们的 POST 请求,以便它们仅在 运行 将新数据发送到服务器时才执行。
- 删除
shelljs
并仅依靠 Node.JS 自己的 child_process 来调用命令。 - 嵌套我们的回调,以便它们按所需顺序执行。
按照这些步骤,您最终会得到以下文件(在修改的行中添加了数字的注释):
const express = require("express");
const bodyParser = require("body-parser");
const app = express();
// Modification #3
const { exec } = require("child_process");
app.use(express.static("public"));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json()); // Modification #1
app.get("/", function (req, res) {
res.sendFile(__dirname + "/index.html");
});
app.post("/", async function (req, res) {
// Modification #2
const data = req.body.load_data;
// Modification #3 & #4
exec(`./run.sh "${data}"`, async (err, stdout, stderr) => {
if (err) console.error(err)
console.log(stdout);
exec('python csv_to_html.py', (err, stdout, stderr) => {
console.log(stdout);
res.redirect("/");
});
});
});
app.listen(3000, function () {
console.log("Starting server at port 3000...");
});
发送到 POST 路由的请求正文应为 Content-Type application/json
,并且类似于以下内容:
{
"load_data": "argumentForRunSH"
}