Quantcast
Channel: Envato Tuts+ Code
Viewing all articles
Browse latest Browse all 5160

Node.js Succinctly: Introduction to Node.js

$
0
0

What Is Node.js?

According to the Node.js website, “Node.js is a platform built on Chrome's JavaScript runtime for easily building fast, scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices.” I suggest you also read the “What is Node.js” thread for a more in-depth Node.js definition.

Node.js provides the complete solution for server-side applications, such as web platforms. It can communicate with other systems, like database, LDAP, and any legacy application. There are many scenarios we can implement for Node.js. 

Installation

Node.js can run on Windows, Linux, and Mac. It provides 32-bit and 64-bit platforms. To install Node.js for Ubuntu Linux, use the console and write the following script: 

For complete instructions on how to install Node.js for another Linux platform, visit GitHub.

If you are a Windows user, you can install Node.js using the setup file. You can download itfrom the Node.js website. Download the .MSI file according to your platform. Run it and you will get the setup dialog as follows: 

A Nodejs Setup dialog

Follow the installation instructions.

A Node.js icon will be created on the Windows menu after installation. If you click this menu, the Node.js console will open. 

Nodejs console

You can run the Node.js console manually from Windows Command Prompt (CMD). Launch it and type the following: 

It will display a response like the Node.js console. 

Running Nodejs from Command Prompt

Development Tools

You can use any text editor to write Node.js code. If you want to get more development experience, you could use the code editor with rich features such as WebStorm JetBrains, Eclipse, and Visual Studio. Some code editors may provide a debugging feature.

For Visual Studio 2012, you can use a web project template such as ASP.NET Empty Web Site. 

Web project template on Visual Studio 2012

Visual Studio 2012 provides an IntelliSense feature. It can help you to display information about API and syntax. 

IntelliSense feature on Visual Studio 2012

Hello World

Learning a new programming language usually starts with writing “hello world” code. Now how about Node.js? Let’s start writing “hello world” for Node.js.

First, run your code editor and write the following: 

Save this code to a file named helloworld.js.

Then open CMD or Terminal (Linux) and execute this file: 

Here is the sample output of our “hello world” application: 

Hello world application for Nodejs

Node.js Module

Node.js provides modules to help our development. It is possible to cut development time because you don’t need write many lines of code.

There are a lot of Node.js modules that you can use. The list of Node.js modules can be seen on this GitHub page.

To install Node.js modules, you need the Node Packaged Modules (npm) package manager. After installing Node.js, you should have the npm package manager. You can check this by typing the script shown in the following table: 

Then you will get a response on the console as shown in the following figure: 

Calling the npm package manager

Let’s start to use a Node.js module, for instance, to add color to our console text. The module is cli-color.

First, install the cli-color module: 

Note: To install a Node.js module, your computer must be able to access an internet network. Some modules may require administrator level privileges for installation.

The output of installation is shown in Figure 8.

cli-color module installation

How do we use this module?

We need call the Node.js module in our code by calling require with the module name. 

Therefore, if we want to color text, we can call xxx.blue for blue text and xxx.green for green text. xxx is a cli-color object. 

The sample of program output can be seen in Figure 9. 

Program output for Nodejs application with cli-color module

Updating Node.js Version

How do you know the version of Node.js you are working with?

You can use a Node.js command to get the information on your Node.js version. Write this script: 

This is the output of Node.js script: 

Getting the current Nodejs version

Node.js doesn’t automatically update the existing runtime. You should check and update the Node.js version manually. Download the setup file from the Node.js website and then install it on your computer. 

The setup installation will check the installed Node.js. If it was found, it will update the current version of Node.js. Check your Node.js version again after installation is complete. 

This tutorial represents a chapter from Node.js Succinctly, a free eBook from the team at Syncfusion.


Viewing all articles
Browse latest Browse all 5160

Trending Articles