[英]Update HTML object with node.js and javascript
I'm new to nodejs and jquery, and I'm trying to update one single html object using a script. 我是nodejs和jquery的新手,并且正在尝试使用脚本更新一个html对象。 I am using a Raspberry pi 2 and a ultrasonic sensor, to measure distance.
我正在使用Raspberry pi 2和超声波传感器来测量距离。 I want to measure continuous, and update the html document at the same time with the real time values.
我想进行连续测量,并同时使用实时值更新html文档。
When I try to run my code it behaves like a server and not a client. 当我尝试运行我的代码时,它的行为就像服务器而不是客户端。 Everything that i console.log() prints in the cmd and not in the browesers' console.
我console.log()的所有内容都在cmd中打印,而不是在浏览器的控制台中打印。 When I run my code now i do it with "sudo node surveyor.js", but nothing happens in the html-document.
现在,当我运行代码时,我使用“ sudo节点surveyor.js”来执行此操作,但是html文档中什么也没有发生。 I have linked it properly to the script.
我已将其正确链接到脚本。 I have also tried document.getElementsByTagName("h6").innerHTML = distance.toFixed(2), but the error is "document is not defiend".
我也尝试了document.getElementsByTagName(“ h6”)。innerHTML = distance.toFixed(2),但错误是“文档不被使用”。
Is there any easy way to fix this? 有没有简单的方法可以解决此问题?
My code this far is: 到目前为止,我的代码是:
var statistics = require('math-statistics');
var usonic = require('r-pi-usonic');
var fs = require("fs");
var path = require("path");
var jsdom = require("jsdom");
var htmlSource = fs.readFileSync("../index.html", "utf8");
var init = function(config) {
usonic.init(function (error) {
if (error) {
console.log('error');
} else {
var sensor = usonic.createSensor(config.echoPin, config.triggerPin, config.timeout);
//console.log(config);
var distances;
(function measure() {
if (!distances || distances.length === config.rate) {
if (distances) {
print(distances);
}
distances = [];
}
setTimeout(function() {
distances.push(sensor());
measure();
}, config.delay);
}());
}
});
};
var print = function(distances) {
var distance = statistics.median(distances);
process.stdout.clearLine();
process.stdout.cursorTo(0);
if (distance < 0) {
process.stdout.write('Error: Measurement timeout.\n');
} else {
process.stdout.write('Distance: ' + distance.toFixed(2) + ' cm');
call_jsdom(htmlSource, function (window) {
var $ = window.$;
$("h6").replaceWith(distance.toFixed(2));
console.log(documentToSource(window.document));
});
}
};
function documentToSource(doc) {
// The non-standard window.document.outerHTML also exists,
// but currently does not preserve source code structure as well
// The following two operations are non-standard
return doc.doctype.toString()+doc.innerHTML;
}
function call_jsdom(source, callback) {
jsdom.env(
source,
[ 'jquery-1.7.1.min.js' ],
function(errors, window) {
process.nextTick(
function () {
if (errors) {
throw new Error("There were errors: "+errors);
}
callback(window);
}
);
}
);
}
init({
echoPin: 15, //Echo pin
triggerPin: 14, //Trigger pin
timeout: 1000, //Measurement timeout in µs
delay: 60, //Measurement delay in ms
rate: 5 //Measurements per sample
});
Node.js is a server-side implementation of JavaScript. Node.js是JavaScript的服务器端实现。 It's ok to do all the sensors operations and calculations on server-side, but you need some mechanism to provide the results to your clients.
可以在服务器端执行所有传感器的操作和计算,但是您需要某种机制来将结果提供给客户。 If they are going to use your application by using a web browser, you must run a HTTP server, like Express.js , and create a route (something like http://localhost/surveyor or just http://localhost/ ) that calls a method you have implemented on server-side and do something with the result.
如果他们要通过网络浏览器使用您的应用程序,则必须运行HTTP服务器(如Express.js )并创建一条路由(如http:// localhost / surveyor或仅http:// localhost / )调用您已在服务器端实现的方法,并对结果执行某些操作。 One possible way to return this resulting data to the clients is by rendering an HTML page that shows them.
将结果数据返回给客户端的一种可能方法是渲染一个显示它们的HTML页面。 For that you should use a Template Engine .
为此,您应该使用模板引擎 。
Any DOM manipulation should be done on client-side (you could, for example, include a <script>
tag inside your template HTML just to try and understand how it works, but it is not recommended to do this in production environments). 任何DOM操作都应在客户端进行(例如,您可以在模板HTML中包含
<script>
标记,只是为了尝试了解它的工作方式,但不建议在生产环境中执行此操作)。
Try searching google for Node.js examples and tutorials and you will get it :) 尝试在Google上搜索Node.js示例和教程,您将得到它:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.