3

I am writing a simple Helloworld example that can be found everywhere on the internet:

[nodejs.php] [Location: localhost/nodejs.php]

<!DOCTYPE HTML>
<html>
    <head>
        <script type="text/javascript">
            var http = require('http');

            var server = http.createServer(function (request, response) {
              response.writeHead(200, {"Content-Type": "text/plain"});
              response.end("Hello World\n");
            }).listen(8000);

            console.log("Server running at http://127.0.0.1:8000/");
        </script>
    </head>


</html>

The code is pretty straightforward and understandable. However, when I tried to run the code, the following error appeared:

Uncaught ReferenceError: require is not defined 

I understand the message means that there is no function such as require() in my page. Of course there is not. I went over Google so many times again and tried different tutorials to find out whether I needed to embed any kind of nodejs.js file in the webpage or not, but all tutorials seem to not mention about this, and I have tried entering the Nodejs folder. I found a lot of files like edit.js, adduser.js, bin.js... and I am officially stuck. I went over and over what Nodejs really is, and why the example doesn't work, but there are not too many supports from Google. So far, as I understand:

  • nodejs is a javascript toolkit that is event-driven, non-blocking I/O
  • nodejs allows users to access backend coding using javascript
  • nodejs parses V8 Google Chrome javascript engine
  • nodejs can create concurrent server applications
  • In my assumption, nodeJS is a framework (it is likely wrong) which connects between server and client to allow backend access

Please DO correct me if I am wrong, I really appreciate that. I need to have more knowledge about this subject while there are not many resources out there.

My questions are:

  1. If NodeJS is a javascript library, how can I embed it to my website? Is there any different from localhost versus online hosting?
  2. I often see people dealing with listen(8000), what is the port about? Which port should I choose?
  3. Is Socket.io the same as NodeJS in what it is, how to install it (I understand that Socket.io runs on Nodejs)? Is Socket.io with NodeJS as same as jQueryUI with jQuery abstractly? For future reference, when I would like to use another library with NodeJS, what are the standard protocols to do so?

Thank you everyone,

Tim.

cuzmAZN
  • 377
  • 1
  • 7
  • 25
  • 3
    node.js works on the server side, not in the browser – soulcheck Aug 10 '13 at 19:57
  • I have read that comments (node.js works on the server side) a lot of times on the Internet, but it is not clear to me. Can you go deeper, or explain how I can make it work as in the server? – cuzmAZN Aug 10 '13 at 19:58
  • @cuzmAZN You need the NodeJS platform built on a JavaScript engine, it runs on the server-side (Instead of PHP) and lets you return HTTP responses. It has no script tags, or explicit modeling of HTML. The book I referenced you to in the previous comments is a good and acclaimed introduction - it covers the basics of a NodeJS server and it's where you should start in my opinion... your question sounds like you did put a lot of effort into looking into it but sounds very misguided. – Benjamin Gruenbaum Aug 10 '13 at 20:00
  • Q1. See soulcheck. To be very clear on that, you would use node.js instead of PHP AND a webserver. Q2. The port refers to the webserver port: where it is listening for traffic. Q3. No. NodeJS is very different from all of those. – CodeBeard Aug 10 '13 at 20:00
  • Ah. So is it like NodeJS (in the foolish way of explanation) is just like PHP. I mean, instead of PHP, you use NodeJS? And instead of finding a PHP host, you need a NodeJS host? If so, where is the htdocs (or anything similar) folder of NodeJS after installing it that I can start? And does it run Localhost? I'm currently using XAMPP. – cuzmAZN Aug 10 '13 at 20:08
  • @cuzmAZN Exactly, (localhost is just the name of your computer's loop-back, you can run node locally - yes). perhaps you should [read my question here](http://stackoverflow.com/questions/11311672/building-a-website-using-node-js-best-practice). – Benjamin Gruenbaum Aug 10 '13 at 20:35

3 Answers3

7

Do I need to embed NodeJS?

No. You need to run it using Node and not using a web browser.

If NodeJS is a javascript library

It isn't

how can I embed it to my website?

You can install it on your webserver and run it there (server side).

Is there any different from localhost versus online hosting?

No

I often see people dealing with listen(8000), what is the port about?

If you are running a TCP/IP server, it has to listen on a port so that packets can be sent to it.

Which port should I choose?

One that isn't in use by something else

Is Socket.io the same as NodeJS

No. It comes in two parts. One part runs on the server with Node, the other part runs in the browser.

how to install it?

There are instructions on the website.

Is Socket.io with NodeJS as same as jQueryUI with jQuery abstractly?

No

For future reference, when I would like to use another library with NodeJS, what are the standard protocols to do so?

CommonJS

So is it like NodeJS (in the foolish way of explanation) is just like PHP. I mean, instead of PHP, you use NodeJS?

In simple terms, yes.

And instead of finding a PHP host, you need a NodeJS host?

Possibly. I don't know if anyone offers NodeJS hosting per se. I use VPSs when I want to use Node.

If so, where is the htdocs (or anything similar) folder of NodeJS after installing it that I can start?

NodeJS isn't a web server. It is a means to run JavaScript. You can write a web server in JavaScript and run it via Node, which is what the code in your question does. There isn't an htdocs folder for that code because it always returns the same, hard coded, response. If you want it to read files from the file system based on the URL that was requested, then you would need to add that functionality (by examining the request object and using filesystem module.

And does it run Localhost?

It runs on whatever network interfaces you want it to. See the documentation for the listen() method. Since you haven't passed a hostname argument, it will listen on all interfaces (including localhost).

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • Hi. What do you mean exactly by "and not using a browser"? And how would I determine whether a port is used or not? I'm sorry, those concepts are new to me. – cuzmAZN Aug 10 '13 at 20:13
  • 1
    You use Node instead of Chrome / Firefox / Opera / Safari / Internet Explorer / etc – Quentin Aug 10 '13 at 20:16
  • 1
    You generally determine that a port is free by knowing what server software you are running on the computer you are running the server on. – Quentin Aug 10 '13 at 20:17
  • I'm quite clear on the port now. But about the Chrome, if it is design for webpage, and you don't use browser, how can you display those contents and interact with it? It is very confusing to me. – cuzmAZN Aug 10 '13 at 20:20
  • 1
    You output data to the command line, or use a library that [provides a GUI like GTK](https://npmjs.org/package/gui), or listen on the network and have the output done to a network client (e.g. a web browser using the HTTP protocol). – Quentin Aug 10 '13 at 20:24
2

The intro example is explained on http://nodejs.org/ :

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');

To run the server, put the code into a file example.js and execute it with the node program from the command line

% node example.js
Server running at http://127.0.0.1:1337/

Write the code block to the file example.js (no HTML), run it with node example.js, open a browser and go to http://127.0.0.1:1337/

SheetJS
  • 22,470
  • 12
  • 65
  • 75
  • Thank you very much for your reply. I comprehended until that point. However, it is too unclear to me that how can I make it and my webpage connects? I understand now (thanks to all comments above) that NodeJS is simply a server like Apache or XAMPP, but I don't know how to get started as it is a whole new method. For example, I have a file .html at localhost/index.html, now, how can I start to use Node functions from there? What should I do? Do I need to turn on Node? And what is the point of server.js (hello world example) if it can't interact with my webpage (using AJAX or whichever)? – cuzmAZN Aug 11 '13 at 03:41
  • @cuzmAZN if you just want to host static files (like HTML), the inner function should read the desired file and write the data in the response. See https://gist.github.com/rpflorence/701407 for a simple example – SheetJS Aug 11 '13 at 04:37
  • Not only static, I mean to have my javascript in that HTML file to interact with server.js. That is what really confuses me. – cuzmAZN Aug 11 '13 at 05:07
  • 1
    Whenever you make a request to `http://127.0.0.1:1337/` it will automatically request the file from the server. For example, if you want to respond to the query `/foo`, you can check if url.parse(request.url).pathname is "/foo" – SheetJS Aug 11 '13 at 05:12
  • Ahhhh! I see. So is that the port it is listening to that really makes the connection between my javascript and the server? – cuzmAZN Aug 11 '13 at 05:19
  • 1
    Yep, and the only reason you don't normally see it with apache is that the default is port 80 (the standard web port) – SheetJS Aug 11 '13 at 05:20
  • I understand now clearly about that. Thank you very much. If only I could vote for 2 answers as official answers. – cuzmAZN Aug 11 '13 at 05:23
0

Node is serverside, not clientside. You need to run it on whatever machine is serving these files. So no, you can't embed it on some page. Look up how to download and configure it here: http://nodejs.org

gr3co
  • 893
  • 1
  • 7
  • 15