How to include nodejs modules in html files?

后端 未结 3 684
误落风尘
误落风尘 2021-02-05 11:36

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??

My js file looks like this.



        
相关标签:
3条回答
  • 2021-02-05 11:49

    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).

    0 讨论(0)
  • 2021-02-05 11:51

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

    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.

    0 讨论(0)
  • 2021-02-05 12:00

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

    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.

    0 讨论(0)
提交回复
热议问题