如何使用html按钮执行节点功能 [英] How to execute node function using html button
问题描述
我是nodejs的新手,我想使用节点写入串行端口,但现在我想让它由一个按钮触发。数据来自文本框。
当我在控制台/终端中运行它时,我的节点脚本运行良好。但不能用网页上的按钮来完成。
这里是我嵌入html中的nodejs
<!DOCTYPE html>
< html>
< head>
< title>节点x HTML< / title>
< / head>
< body>
< script>
函数write_serial()
{
var serialport = require(serialport);
var SerialPort = serialport.SerialPort;
var sp = new SerialPort(/ dev / ttyACM0,{
baudrate:9600,
parser:serialport.parsers.readline(\ n)
});
var text = document.getElementById(textbox1)。value;
sp.on('open',function()
{
sp.on('data',function(data)
{
sp .write(name);
});
});
}
< / script>
< Input Type =textid =textbox1>
< BR>
< br>< button onclick =write_serial(); HREF = JavaScript的:;!> GO< /按钮>
< / body>
< / html>
这是我打开页面控制台(F12)时得到的错误
ReferenceError:require未定义
预先感谢您的帮助。 :)
Node.js是一个宿主环境,可以执行JS并提供Node.js特定的API。浏览器,是一个不同的托管环境,具有不同的API。您不能在浏览器中使用Node的特定API,并且使用Node API的JS会导致浏览器出错。
例如,您的脚本使用全局需要
函数,这在浏览器API中不可用。这就是为什么:
$ b
ReferenceError:require未定义
相反,您的脚本无法在Node上执行,因为它使用浏览器API:
document.getElementById (textbox1)
您已将来自不同环境的API混合到一个脚本中。
$ b但是,如果您的JS脚本不使用节点或浏览器特定的API,则可以在Node和浏览器中执行,而不会出现错误。但脚本并非如此。
解决您的问题的方法可以是将您的脚本分成两个单独的脚本,在浏览器中运行一个脚本,另一个脚本Node.js,并使用 XMLHttpRequest 在它们之间交换数据。
I'm new at nodejs and I want to write to a serial port using node but now I want it to be triggered by a button. And the data is coming from a textbox. My node script is doing fine when I run it in the console/terminal. But can't do it with a button in a webpage. Here's my nodejs embedded in html
<!DOCTYPE html>
<html>
<head>
<title>Node x HTML</title>
</head>
<body>
<script>
function write_serial()
{
var serialport = require("serialport");
var SerialPort = serialport.SerialPort;
var sp = new SerialPort("/dev/ttyACM0", {
baudrate: 9600,
parser: serialport.parsers.readline("\n")
});
var text = document.getElementById("textbox1").value;
sp.on('open', function()
{
sp.on('data', function (data)
{
sp.write(name);
});
});
}
</script>
<Input Type = "text" id = "textbox1" >
<BR>
<br><button onclick="write_serial();" href="javascript:;">Go!</button>
</body>
</html>
Here's the error I got when I open the console of the page (F12)
ReferenceError: require is not defined
Thanks in advance for your help. :)
Node.js is a hosting environment, that can execute JS and provides Node.js specific API. Browser, is a different hosting environment, with different API's. You can't use Node's specific API in a browser, and JS that uses Node API will result in an error in a browser.
For example, your script is using global require
function, which is not available in a browser API's. And that's why:
ReferenceError: require is not defined
Conversely, your script can't be executed on Node as well, since it uses browser API:
document.getElementById("textbox1")
You've mixed API's from different environments in one script.
However, if your JS script doesn't use Node or browser specific API, it can be executed in both Node and a browser without an error. But it's not the case with your script.
The solution to your problem can be to split your script into two separate scripts, run one in a browser, and the other in Node.js, and to exchange data between them using XMLHttpRequest.
这篇关于如何使用html按钮执行节点功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!