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:
- If NodeJS is a javascript library, how can I embed it to my website? Is there any different from localhost versus online hosting?
- I often see people dealing with listen(8000), what is the port about? Which port should I choose?
- 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.