Node.js Tutorial For Beginners : Part -1

Node.js is an open source, cross-platform, asynchronous event-driven JavaScript runtime. Node.js can be used to write command line tools and server-side scripts outside of a browser. It is designed to build scalable network applications. You need to use JavaScript programming language to code in Node.js.

How to install ?

To get started with Node.js development, you must first download and install Node.js in your system.

Approach 1 :

The first approach to install Node.js is simple and straight forward. You must visit The official Node.js website and follow the installation instructions here : https://nodejs.org

Approach 2 :

Alternatively, if you are interested in installing Node.js using NVM (node version manager) you must first install NVM . To do so, follow instructions in below link,

  • Click here — Instructions for installing NVM on Linux and MAC platform.
  • Click here — Instructions for installing NVM on Windows platform.

The advantages of installing Node.js via NVM include having multiple versions of Node.js in your machine and can easily switch between different versions.

Once the NVM is installed in your machine, you simply run the following command to install Latest Node.js in your machine.

nvm install latest 

or alternatively run the following command to install specific version of Node.js. The version can be a node.js version. Set [arch] to “all” to install 32 AND 64 bit versions.

nvm install <version> [arch] 

Verify Installation

If you are done installing Node.js in the previous step. To check which version of Node you are currently using run the following command in command prompt.

node -v or
node --version

alternatively, if you are using NVM, you can check by running nvm list command as below. The highlighted version of the Node.js is the one you are actively using.

Running Node REPL

To run the Read–eval–print loop(REPL) in node. you can just enter node command. you can evaluate REPL as shown below.

Node- REPL

Running your first Node.js program

Let’s create a folder called Node-Tutorials in our system.

Example 1 : Running REPL code from file.

Create a folderChapter1 under Node-Tutorial. Createindex.js file under Chapter1which will have following code.

Now run the file by typing command,node index.js and you will see the following output.

REPL like output when code is run from a file.

Example 2 : Running a node server to serve on web browser.

Create a folderChapter2 under Node-Tutorial. Createserver.js file under Chapter2which will have following code.

Now run the file by typing command,node server.js and you will see that the code is now being served at http://127.0.0.1:8000. If you access the link on browser, you should see the following.

Node can be used to create a server and serve content on web

Let’s explore Chapter2 in bit more detail.

  • Line #1 we imported http built in node module by using require. This is how we import files and libraries require ‘d for execution in Node.js
  • Between line#6 to line#10, we made use of createServer method from http library accept request and serve the response.
  • Line #12, the server we created is being made to listen to the requests.

Conclusion

Node.js is easy to learn if you have prior experience in coding in JavaScript. It’s fast, flexible and fun to wok with. You can build scalable application using Node.js.

If you simply wish to try out the code we used in this tutorial. Please fork the below GitHub repository.

Fullstack Developer. Loves to write about Javascript, React.js, Node.js , Cloud Platforms.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store