20

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.

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

Uncaught ReferenceError: require is not defined

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

Thanks in advance.

1
  • 7
    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. Commented Apr 15, 2012 at 21:37

3 Answers 3

20

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

Sign up to request clarification or add additional context in comments.

7 Comments

Can you provide any link to such examples where I can find such communication between server/client
@FarooqArshed: It's hard to do a search relate to web programming without tripping over ajax examples. It would literally be a disservice to cherry-pick them for you.
@t-j-crowder If you have a module that you want to use on the backend (via node.js) and also on the front end (with script in html), you don't want to maintain two versions -- you just want to have a single .js file for both cases. The problem is that the node version has a exports/requires which trigger errors on client code. See this question
@wcochran: I'm not really seeing how that's related here, but yes, completely agreed on not doing multiple versions. So you use any of the several AMD systems that provide browser and Node compatibility.
The server parses html (php), I don't see why it wouldn't be able to interpret included javascript serverside. You could have such a server script even modify dom before it's sent to the client. Isn't there a solution like this?
|
5

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.

1 Comment

I would be thankful if you can provide any link to such examples where I can find such communication between server/client.
3

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.