Javascript 如何在 html 文件中包含 nodejs 模块?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10166324/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 00:08:13  来源:igfitidea点击:

How to include nodejs modules in html files?

javascriptnode.jsrequire

提问by Farooq Arshed

First of all I am new to nodejs and secondly below is my question. How to include nodejs net module in js which is loaded in html??

首先,我是 nodejs 的新手,其次是我的问题。如何在 html 中加载的 js 中包含 nodejs 网络模块?

My js file looks like this.

我的 js 文件看起来像这样。

net = require('net');
var client = net.createConnection(8000, '192.168.15.59');
client.on('connect',function(){
console.log('Connected To Server');
});
client.on('data',function(data){
console.log('Incoming data:; ' + data);
});

And my html file is below

我的 html 文件在下面

<html>
<head>
<script type="text/javascript" src="sample.js"></script>
<script type="text/javascript">
function displaymessage(message)
{
alert(message);
client.write(message, encoding='utf8')
}
</script>
</head>

<body>
<form>
<input type="text" id="msg"></input>
<input type="button" value="Click me!" onclick="displaymessage(document.getElementById('msg').value)" />
</form>
</body>
</html>

When I run the HTML file in browser it gives below error

当我在浏览器中运行 HTML 文件时,它给出以下错误

Uncaught ReferenceError: require is not defined

未捕获的 ReferenceError:需要未定义

whereas if I run the js file directly in nodejs (like this "node sample.js) using command line then it works fine.

而如果我使用命令行直接在 nodejs 中运行 js 文件(比如这个“node sample.js),那么它工作正常。

Thanks in advance.

提前致谢。

回答by T.J. Crowder

NodeJS runs on the server. Script inside HTML files runs on the client. You don't include server code on the client. Instead, you send messages to the server code from the client, and interpret the results. So the standard way to do this is to define a resource on the server that generates the content or data you want to generate, and to retrieve that content or data from the client, using just normal page loading or "ajax" (although these days, most people don't use the "x" [XML] in "ajax" [some still do], they use JSON, text, or HTML).

NodeJS 运行在服务器上。HTML 文件中的脚本在客户端上运行。您不在客户端包含服务器代码。相反,您从客户端向服务器代码发送消息,并解释结果。因此,执行此操作的标准方法是在服务器上定义一个资源来生成您想要生成的内容或数据,并从客户端检索该内容或数据,仅使用正常的页面加载或“ ajax”(尽管现在,大多数人不使用“ajax”中的“x”[XML] [有些人仍然使用],他们使用JSON、文本或 HTML)。

回答by Travis Webb

To clarify what @T.J.Crowder said in the comment: What you are trying to do is impossible.

澄清@TJCrowder 在评论中所说的话:你想要做的事情是不可能的。

NodeJS is a server-side framework. The Javascript you write in NodeJS is executed on the server. The Javascript you write for your HTML pages is executed on the client. The client and server can not call each other's methods directly. This is what AJAX and other asynchronous client-server communication techniques are used for.

NodeJS 是一个服务器端框架。您在 NodeJS 中编写的 Javascript 在服务器上执行。您为 HTML 页面编写的 Javascript 在客户端上执行。客户端和服务器不能直接调用彼此的方法。这就是 AJAX 和其他异步客户端-服务器通信技术的用途。

回答by lyonplus

The reason "require is not defined" is because "require" is a keyword of node.js, but it is not a keyword in browser.

之所以“require 没有定义”是因为“require”是node.js 的一个关键字,而在浏览器中不是关键字。

Node.js is a virtual machine (or running context) for javascript, browser is also a virtual machine for javascript. But they are hugely different. You cannot use a keyword supported in one virtual machine in another virtual machine, just like you can use C/C++ on both Windows and Linux, but there are many libraries that are either in Linux only or Windows only.

Node.js 是 javascript 的虚拟机(或运行上下文),浏览器也是 javascript 的虚拟机。但它们有很大的不同。您不能在另一台虚拟机中使用一个虚拟机支持的关键字,就像您可以在 Windows 和 Linux 上使用 C/C++ 一样,但是有许多库要么仅在 Linux 中,要么仅在 Windows 中。