Node.js

A Gentle Introduction to Node.js

Recently at work, I sought to familiarize myself with Node.js. Naturally, I started searching the pool of applications in Keyhole’s GitHub repository. This was the first time I was using Node.js and the instructions in the application readme file to run the application locally was thoroughly detailed.

However, I ran into some very basic questions trying to run the application locally. Based on my experience, I drafted these fundamental instructions presented in this post. I hope these instructions assist to get you started with Node.js.

What is Node.js?

Node.js is an open-source, cross-platform JavaScript runtime built on Google Chrome’s V8 JavaScript engine.

Basically, it provides another way to run JavaScript on your computer. Node.js is lightweight and uses an event-driven, non-blocking I/O model. Node.js is used by thousands of developers to develop I/O intensive web applications, single-page applications, and other web applications.

Installing Node.js on Windows 10

Node.js is very easy to install in two steps:

  1. Download the Windows executable file from https://nodejs.org/en/download/.
  2. Run the installer and follow the instructions on the screen.

Once installed, you will be able to launch Node.js command prompt from Windows Menu –> All apps –> Node.js –> Node.js command prompt. Alternatively, you can open a command prompt, type node, and press enter to get an interactive shell that will enable you to execute JavaScript code.

If you get an error, I recommend the following troubleshooting steps:

  1. Open the Setting from the Windows Menu. Search for edit system environment variables and open. Select Environment Variables. Under System variable check the value for the Path variable. Make sure it contains a path to the Node Application. (C:\Program Files\nodejs\).
  2. If you are still getting error, make sure you restart the command window after Node.js is installed.
  3. If the Path variable was created during the installation of Node.js, you will need to restart the computer.

Now What?

You can execute commands on the node command prompt. See below for an example.

Gentle_Introduction_to_Node1

I used the console.log(“HelloWorld”) command. You can see the message displayed on console. Node.js prints a return value of each command. Since console.log does not return anything, undefined is printed at the bottom. To exit the interactive shell, press key ctrl + c.

You can also run commands from a JavaScript file. Create a file helloWorld.js with console.log(“Hello World”); as the content using Notepad. To save a Notepad file as a JavaScript file, select File->Save As->select All files (*.”) for Save as type. Enter the filename with .js extension.

In Node.js command prompt, navigate to the location of the file and enter helloWorld.js. Node,js will run the JavaScript in the file. Results will be displayed on your console. Please note, that you can omit the .js extension as Node.js automatically assumes the file to be in .js format.

Gentle_Introduction_to_Node2

Printing output on the console is not all that fun. Let’s see how we can run a very basic web application. First, create a file helloWorldHttp.js using the following content:

var http = require('http');

var server = http.createServer(function(req, res) {
  res.writeHead(200);
  res.end('Hello World');
});
server.listen(8080);

To run the file, type node helloWorldHttp.js and press enter.

Gentle_Introduction_to_Node3

You will notice when you run the server, the program does not exit. Node.js will only exit when it is certain there will be no more possible event(s).

In this tutorial, a HTTP server is open that will be serving to the requests from the clients, generating events.

To test the server, open a web browser and type http://localhost:8080/ in the address bar and hit enter. The web browser will display Hello World.

Gentle_Introduction_to_Node4

Node.js is very basic and does not perform various functions out-of-the-box but has a wide set of built-in modules that provides different functions. The HTTP module makes it easy to create a basic HTTP server.

Let’s look at the JavaScript syntax in the example file to create the server.

The first line is using core module HTTP by require (“http”) and assigning it to the variable http to use the functions in module. The second line is using module’s function createServer to create the server. The syntax is passing a function, here an anonymous function, as a parameter in the createServer function. The anonymous function simply takes two parameters, req (request) and res (response). On any request, the function is serving back the response with header 200 (ok) and the text Hello World in the body. The third line dictates the port the server listens to, the example used port 8080. Thus no matter what you enter on the address bar after localhost:8080/xyz, it will always display the hello world text.

Conclusion

A gentle introduction to Node.js provides a starting point to Node.js. Hopefully from this point, you will be able to build on these instructions and review tutorials available out on the web.

Reference: A Gentle Introduction to Node.js from our WCG partner Keyhole Software at the Keyhole Software blog.

Jinal Patel

Jinal Patel is a Software Consultant at Keyhole Software. She has worked with .NET, Java, and JavaScript/SPA technologies and loves a challenge. Before she was a coder, she was in biomedical engineering.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
some
some
8 years ago

Node is made for the Unix like OSes, stop using it in Windows Loser Land

Back to top button