Skip to content Skip to sidebar Skip to footer

Understanding Some Aspects Of Node.js

There are a couple of aspects of node.js I don't quite understand. I hope someone can make things clearer When you install node.js where do you store your files so that the web bro

Solution 1:

When you install node.js where do you store your files so that the web browser can display your content? For example Apache have a www folder.

Wherever you want. node.js doesn't serve static content, it runs JavaScript. You tell it which script to run when you start it.

You could write some JavaScript that serves static content, but where you would keep it depends on the code you wrote.

Does node.js replace client side javascript?

Only in so far as any server side programming replaces client side JavaScript.

One advantage of using JS on the server side is that you can reuse libraries on both the client and the server. See Mojito for a framework that claims to focus on this (I haven't had time to try it myself yet).

For example if I wanted to put data from the server into this div element <div id="content"></div> In PHP you could do something like this: <div id="content"><?php echo $content; ?></div>

PHP is a template language with an embedded programming language. JavaScript is a programming language. Typically you would use a template language (e.g. moustache) from your JS.

Would you ever call node.js from client side? For example: An Ajax request to node.js to get data.

Yes, if you want to. Just like any other server side programming environment. (Assuming you are using node to run an HTTP server).

Solution 2:

Node.js is not a server (like e.g. Apache). It's a platform to run Javascript with some built in libraries (so-called modules). It is very easy to write server (HTTP or any other) in Node.js, but you can also write completely different programs (no network related, meant to be executed locally).

I suggest you read this: http://www.nodebeginner.org/. It took me several hours but allowed me to understand basics of Node without much pain.

As for client side scripting, it's generally separate. Code in Node runs in separate environment then the one in browser. They can communicate, but you have to explicitly make them to. It's not much different from server side coding in PHP. The code on server produces some output (eg. HTML) which is sent to client. If there are scripts in output, client (browser) executes it. They can communicate (via XHR, websockets, etc.), but by itself those scripts are separate.

Solution 3:

How does node.js interact with HTML? For example if I wanted to put data from the server into this div element In PHP you could do something like this:

You would probably send the content as JSON to the JS-Client and insert it into the DOM (using plain JS or JQuery).

I've wrote a REALLY trivial (and not quite feature-rich :-P) chat application in Node.js a while a go, to try out some concepts and understand working with JS on both the client and server. Maybe it will give you some clues.

EDIT

In this application, the server also serves static files, which you should not do when implementing production ready application (Node is not really suited for serving static files!).

Post a Comment for "Understanding Some Aspects Of Node.js"