4th Street Bar Hive-Bar

Hive-Bar Powered by Hive beta-fdb5b5b

Community post

ghanto's node.js tutorial for begginers - part 1

http://hazelcast.org/wp-content/uploads/2016/04/nodejs-vert.png


Do you want to run your own multiplayer game? Or are you bored Apache http server user and want to write your own, better one and run it in a cloud?


In a nutshell, you can do everything using node.js: simple website, advanced game servers, software for robots, cloud services based on the microservices architecture, desktop applications (for example Slack or Visual Studio Code are built on one of node.js frameworks)  and whatever you can think about. Really.

http://mrwgifs.com/wp-content/uploads/2013/07/Ron-Swanson-Excited-In-The-Office-On-Parks-Recreation_408x408.jpg


Excited? Read further.


What do you need?


You can download node.js for any operating system using this link: https://nodejs.org/en/download/current/.  After installation (which is simple, there is no any weird steps), open command line and just type: node -v command. It should produce following output:

https://i.imgsafe.org/68c4b193f9.png
That means that all is working fine.


You need a text editor also. I prefer notepad++ or Visual Code Studio if you use Windows. If you use Linux, you probably have your favorite text editor :)


So, let’s go to write simple application!

First program


We’ll create a simple program to show you, how to run code from files stored on your hard disk.
We write JavaScript code in node.js. But don’t scary, this is not the same JavaScript as you learn on computer science lessons in school. We aren’t going to write pop-ups for an internet browser or “to program” website background color change on click.


Create myscript.js file and put following content inside:

https://i.imgsafe.org/68c4b88464.png


And then go to this directory into command line and run node myscript.js command. At output you should get text similar to this:

https://i.imgsafe.org/68c4955ec6.png


It prints given text, we used built-in variable called __dirname to get a directory path where we created a myscript.js file.


More advanced program


Ok, it was very simple. Let’s assume that you have your website ready to publish and you want to expose it for testing to your friends, from your computer.


What do you do when you don’t know node.js? You’re downloading xampp (~150MB), install all the stuff that it provides, copy site to the htdocs directory and run apache server from the control panel.


What you do, when you know node.js? It’s simple: you write your own web server that have...4 lines of code, lol.
We’ll use express module that have many useful features already implemented. To install this module, type in the command line:

https://i.imgsafe.org/68c491b87e.png

It will create a node_modules directory that contains all external modules that you use.
In the next step, create a server.js file and type:

https://i.imgsafe.org/68c4a566ec.png


What does it do?

  1. we say that we want to use express module
  2. create new express application that is ready to work webserver
  3. make all files from the public directory accessible via browser
  4. run server on port 3000

then create the public directory and put index.html with following content there:

https://i.imgsafe.org/68c4c487ed.png


Save it, run node server.js command and go to http://localhost:3000 in your browser.

https://i.imgsafe.org/68c49ee29b.png


It’s amazing, isn’t it?


What next?


I created this short article to encourage you to use node.js and to show you its power.


Do you want to write a multiplayer game? If you are interested to create one, I’ll write next parts of tutorial - leave a comment then.
 

9 upvotes $0.00

Replies (4)

Review before signing

Posting as . Signing with . Keychain permission: Posting. Hive Keychain will ask you to approve this action next.


  
Technical details

Operation fingerprint: