英文:
Change the color of a led on html
问题
这是您要翻译的部分:
style.css
.led-green {
position: relative;
right: -50%;
top: 10%;
margin: 0 auto;
width: 24px;
height: 24px;
background-color: #ABFF00;
}
.led-red {
position: relative;
right: -50%;
top: 10%;
margin: 0 auto;
width: 24px;
height: 24px;
background-color: rgb(255, 0, 0);
}
app.js
/** 检查互联网连接是否可用 */
var internetConnection = "";
require('dns').resolve('www.google.com', function(err) {
if (err) {
console.log("没有互联网连接");
internetConnection = "led-red";
} else {
console.log("已连接到互联网");
internetConnection = "led-green";
}
console.log(internetConnection);
});
index.ejs
<div class="<%=internetConnection%>"></div>
Update
res.render('index', {
// ...
internetConnection: internetConnection,
// ...
});
UPDATE
问题不在于渲染。问题是当我将div定义如下时,HTML页面不起作用:
<div class="<=%internetconnection%>"></div>
有没有任何解决方法?再次感谢任何提示!
英文:
I'm to change the color of led based on whether of internet connection is there or not.
here's what I have done so far:
style.css
.led-green {
position: relative;
right: -50% ;
top: 10%;
margin: 0 auto;
width: 24px;
height: 24px;
background-color: #ABFF00;
}
.led-red {
position: relative;
right: -50% ;
top: 10%;
margin: 0 auto;
width: 24px;
height: 24px;
background-color: rgb(255, 0, 0);
}
app.js
/** Check if internet connection is avaiable */
var internetConnection =""
require('dns').resolve('www.google.com', function(err) {
if (err) {
console.log("No internet connection");
internetConnection ="led-red";
} else {
console.log("Connected to internet ");
internetConnection ="led-green";
}
console.log(internetConnection)
});
I guess this is the part where I got the problem
index.ejs
<div class="<%=internetConnection%>"> </div>
my hope was that by starting the app the division would be set like :
<div class ="led-green"> </div>
or
<div class ="led-red"> </div>
any idea how may I solve this?
thanks in advance !
Update
I'm the variable in the render function as follows :
.......................
res.render('index', {
.....
internetConnection:internetConnection,
...............
}
UPDATE
the problem isn't the rendering. the issue is that the html page doens't work when I define div as followed :
<div class="<=%internetconnection>"> <div>
any idea how to solve this ?
thanks again for any hint !
答案1
得分: 2
可能你的响应在你的应用程序决定互联网连接是否可用之前就被呈现了吗?看起来像是你面临的问题,因为Node.js是异步的。
尝试将你用于向客户端发送响应的代码放在dns.resolve()
回调中,像这样:
var internetConnection = "";
require('dns').resolve('www.google.com', function(err) {
if (err) {
console.log("没有互联网连接");
internetConnection = "led-red";
ejs.render(errorHTMLString, data, options);
} else {
console.log("已连接到互联网");
internetConnection = "led-green";
ejs.render(noErrorHTMLString, data, options);
}
});
希望这有所帮助。
英文:
Is it possible that your response is rendered before your app decides whether internet connection is available or not? Seems to me like a problem you are facing because nodejs is asynchronous.
try to put the code you are using to send response to the client in dns.resolve() callback, like:
var internetConnection =""
require('dns').resolve('www.google.com', function(err) {
if (err) {
console.log("No internet connection");
internetConnection ="led-red";
ejs.render(errorHTMLString, data, options);
} else {
console.log("Connected to internet ");
internetConnection ="led-green";
ejs.render(noErrorHTMLString, data, options);
}
});
Hope this helps.
答案2
得分: 1
请确保在resolve()
的回调函数内调用render()
函数:
var internetConnection = "";
require('dns').resolve('www.google.com', function(err) {
if (err) {
console.log("没有互联网连接");
internetConnection = "led-red";
} else {
console.log("已连接到互联网");
internetConnection = "led-green";
}
console.log(internetConnection);
res.render('index', {
internetConnection: internetConnection,
});
});
英文:
Please make sure that you call the render()
function within the callback of resolve()
:
var internetConnection =""
require('dns').resolve('www.google.com', function(err) {
if (err) {
console.log("No internet connection");
internetConnection ="led-red";
} else {
console.log("Connected to internet ");
internetConnection ="led-green";
}
console.log(internetConnection);
res.render('index', {
internetConnection:internetConnection,
}
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论