How To Start Programming In Node.js?

Hello readers, if u are a JavaScript developer and want to use JavaScript as the backend for your site then give a thought to Node.js!!! Today, we are gonna learn about Node.js and will create some small basic programs like Hello world. But before that let’s have a glimpse of Node.js.

Node.js: The Javascript Runtime

Node.js is a JavaScript runtime built on Chrome’s V8 JavaScript engine. For developers it’s like a miracle which made it possible to use JavaScript as a backend language.

Node.js uses an event-driven and a non-blocking Input/Output model which makes it lightweight and efficient. npm, also called as Node.js’ package ecosystem is the largest ecosystem of open source Node.js libraries in the world.

Node due to its asynchronous event driven JavaScript runtime nature allows us to build scalable network applications. Even in a single “Hello world” example, many connections can be handled concurrently. Upon each connection the callback function is executed, but if there is no work to be done, Node server will sleep.

For Example, here below we have a basic “Hello World” program which creates a basic node server which sends ‘Hello World’ message when its accessed via browser.

Install Node

You can download node.js from here as per your system OS architecture. After Installing node, we need a good editor so that we can easily write and run our programs in node.js. We are gonna use Visual studio code, but you can choose any editor. Here below I’m providing list and download links of some popular editors:

Hello World program

Now let’s create your first hello world program. Open your editor and create a file named hello.js. Now paste the above shown code in that file and save it. To run the node server, open current directory in command prompt or in Terminal of Visual studio code and execute it with node.js:

$ node example.js

Then the following message will be shown in console:

Server running at http://127.0.0.1:3000/

Similarly, you can run any node.js program. Now let’s learn about this program step by step.
In this example, the first line const http = require(‘http’); is a basic JavaScript declaration. It allows to load http module of node and use its methods using http constant.

Second line var hostname = ‘127.0.0.1’, port = 3000; defines two variable hostname and port which will be used further to run node server on defined host and port.

Third statement from line 6 to 10 is using http.createServer(), which method Returns a new instance of http.Server. In this function a callback function is provided which takes two parameters, first one denotes request data and second one denotes response data. Here res.statusCode = 200; sets status code for response to be 200 which denotes success. Similarly, res.setHeader(‘Content-Type’, ‘text/plain’); sets response header to be type of text. And res.end(‘Hello World\n’); sends whatever content is to be send as response, this function takes either string or buffer as parameter.

In line 12 server.listen() function is asynchronous function which makes our node server to listen connections from browser. This function takes three parameters in following order:

port – the port on server upon which our application runs

host – host of our node.js application. By default it is localhost if not mentioned.

callback function – This function is called after every connection requested.

Node REPL: aka Node.js Console

REPL stands for “Read-Eval-Print-Loop (REPL)”. REPL is virtual environment fro Node or we can refer it as Node Shell. It is a quick and easy way to test simple node.js code. To start REPL simply start Command prompt and type node then press Enter. The screen will appear as below:

You can now test pretty much any Node.js expression in REPL. For example if you type 4+5 and press enter, then it will print sum of 4+5 which is 9 in next line. The + operator in REPL also concatenates strings as in browser. For example “Hello” + “World” will print “Hello World”.

You can also define constants or variables and then use in REPL as in browser. You can also type multiple line code in REPl, you have to just press Enter key. For example if you write a function to add two numbers then it will be like as shown in below image.

Exit: To exit from the REPL terminal, you need to press Ctrl + C twice or write .exit and then press Enter.

Here I am providing some important REPL commands in below Table.

REPL Command

Description

.help

Display help on all the commands

tab Keys

Display the list of all commands.

Up/Down Keys

See previous commands applied in REPL.

.save filename

Save current Node REPL session to a file.

.load filename

Load the specified file in the current Node REPL session.

ctrl + c

Terminate the current command.

ctrl + c (twice)

Exit from the REPL.

ctrl + d

Exit from the REPL.

.break

Exit from multiline expression.

.clear

Exit from multiline expression.

Node Package Manager:

Node Package Manager (NPM) is a command line tool that installs, updates or uninstalls Node.js packages in your application. It is also an online repository for open-source Node.js packages. The node community around the world creates useful modules and publishes them as packages in this repository. To know about any node.js package or to find any you can visit their official site https://www.npmjs.com.

NPM is already included with Node like REPL. to know which version of NPM is installed you can use following command in commmand prompt.

$ npm -v

To update NPM you can use following command:

$ npm install npm -g

NPM performs the installation or uninstallation operation in two modes: global and local. In the global mode, NPM performs operations which affect all the Node.js applications on the computer whereas in the local mode, NPM performs operations for the particular local directory which affects an application in that directory only. To install any node.js package in you project use following command.

$ npm install <package name>

Similarly to install any node.js package globally in your system use following command: