我在使用node.js应用程序中的全局变量时遇到一些问题。 我使用:
app.locals.isTestCafeRunning = false;
我认为这里的问题是本地人,我认为此变量仅在浏览器会话内有效,而在全局应用程序内无效。
我想在主要途径中使用此变量:
app.get('/', function(req, res) {
async function getListData(){
var myListOptions = "";
var myListText = "";
arrayTestcaseData = await getMongojson();
/*code cut*/
var inPlaceButton = "";
if(req.app.locals.isTestCafeRunning===false){
inPlaceButton = "<button id=\"myButton\">Testrun starten</button>";
}else{
inPlaceButton = "<strong>Clipcafe in Benutzung!</strong>";
}
myHtmlCode = myHtmlCode.replace("%%BUTTON%%",inPlaceButton);
res.send(myHtmlCode);
}
getListData();
});
我要在其中填充变量
app.post('/clicked', (req, res) => {
//Function start Testcafe?
createTestCafe('localhost', 1337, 1338)
.then(tc => {
testcafe = tc;
const runner = testcafe.createRunner();
req.app.locals.isTestCafeRunning = true;
inputStore.clLogin = req.body.name;
inputStore.clPassword = req.body.pass;
inputStore.metaFolder = '19233456';
inputStore.metaUrl = req.body.channel;
return runner
.src(['tests/temp.js'])
.browsers(myBrowser)
//.browsers('browserstack:Chrome')
.screenshots('allure/screenshots/', true)
.reporter('allure')
.run();
})
.then(failedCount => {
req.app.locals.isTestCafeRunning = false;
console.log('Tests failed: ' + failedCount);
testcafe.close();
startReportGenerator();
res.sendStatus(201);
});
});
但是我想让此全局变量不是本地vor所有用户,所以如果用户1启动TestCafe,则用户2将仅看到“正在使用”。