最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Helloworld example doesn't work - Do I need to embed NodeJS? - Stack Overflow

programmeradmin6浏览0评论

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.

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.

Share Improve this question asked Aug 10, 2013 at 19:56 cuzmAZNcuzmAZN 3872 gold badges8 silver badges25 bronze badges 6
  • 3 node.js works on the server side, not in the browser – soulcheck Commented Aug 10, 2013 at 19:57
  • I have read that ments (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 Commented Aug 10, 2013 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 ments 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 Commented Aug 10, 2013 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 Commented Aug 10, 2013 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 Commented Aug 10, 2013 at 20:08
 |  Show 1 more ment

3 Answers 3

Reset to default 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 es 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).

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

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

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

发布评论

评论列表(0)

  1. 暂无评论