我有一個 Express 服務器在等待我的網站做一些事情。當我的站點執行某些操作時,應該在 Express 服務器上呼叫一個 shell 腳本。問題是:shell 腳本僅在“確認視窗”被接受或拒絕后運行。我希望提取盡快發生。我什至不需要從 Express 服務器獲取任何東西,我只想通知 Express 盡快運行 shell 腳本。
我在網站上有這個代碼:
messaging.onMessage(function (payload){
fetch("http://localhost:9000/testAPI")
.then(res => res.text())
.then(res => console.log("something:" res));
var r = confirm(callingname " is calling.");
if (r == true) {
window.open(payload.data.contact_link, "_self");
} else {
console.log("didn't open");
}
});
我在后端有這個代碼:
var express = require("express");
var router = express.Router();
router.get("/", function(req,res,next){
const { exec } = require('child_process');
exec('bash hi.sh',
(error, stdout, stderr) => {
console.log(stdout);
console.log(stderr);
if (error !== null) {
console.log(`exec error: ${error}`);
}
});
res.send("API is working");
});
module.exports = router;
uj5u.com熱心網友回復:
confirm()正在阻塞,而您只有一個執行緒。這意味著confirm()將為您的應用程式停止世界,阻止fetch()做任何事情。
作為最簡單的修復方法,您可以嘗試延遲confirm()被呼叫的時刻。這將允許fetch()獲取請求。
messaging.onMessage(function (payload) {
fetch("http://localhost:9000/testAPI")
.then(res => res.text())
.then(text => console.log("something:" text));
setTimeout(function () {
if (confirm(`${callingname} is calling.`)) {
window.open(payload.data.contact_link, "_self");
} else {
console.log("didnt open");
}
}, 50);
});
其他選項是放入fetchconfirm()的.then()回呼之一,或使用非阻塞替代confirm(),如評論中所建議的。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/390748.html
標籤:javascript 节点.js 表达 拿来
上一篇:本地主機無限加載nodejs
