【发布时间】:2021-02-02 01:22:45
【问题描述】:
我有一个 express.js 应用程序,它需要在服务器上运行一个脚本,以便稍后使用函数派生一些值。这是它的要点:
shell.exec(commandString);
readFolder();
renderPage();
基本上,我需要在服务器上运行一个脚本,然后运行第二个函数,然后运行第三个函数。这些需要随后发生,但无论我做什么,似乎javascript都会继续使用第二个和第三个函数。我尝试过承诺、异步、回调。所有这些我只是部分理解并且似乎取得了零进展。
我承认我是一个 javascript 新手。我正在和其他人一起做一个项目,这个任务落到了我身上。我怀疑这是实现我们最终目标的最佳方式,但我别无选择。请帮忙。
我会把整个帖子放在这里供参考:
//Run script when post is rec'd from root and send to results page
app.post("/", (req, res) => {
var commandString;
//take values and create complete command for Astrum script
commandString = 'bash /home/astrum/Main/Astrum.sh -s ' + req.body.speed + ' -h ' + req.body.host + ' -u ' + req.body.username + ' -p ' + req.body.password;
//execute command in shell
shell.exec(commandString);
readFolder();
renderPage();
//Iterate thru filenames to create arrays for links and link labels
function readFolder() {
fs.readdir('./reports/html/', (err, files) => {
//variable & method for links to html records pages
ipAddressesLink = files; //this is initialized earlier, globally
//variable and method to remove file extension for link labels in pug
ipAddresses = files.map(removeExtension); //this is initialized earlier, globally
});
}
//function to remove last five characters of each element
function removeExtension(value) {
return value.substring(0, value.length - 5);
};
//function to render the page
function renderPage() {
res.render("results", {ipAddressesLink, ipAddresses, title: 'Results'});
}
res.end();
});
【问题讨论】:
-
shell 来自 shell.js npm 模块
标签: javascript node.js express asynchronous synchronous