How to get started using Node.js

How to get started using Node.js

As a developer, I use Node.js pretty much for all of my development projects. It's fast, efficient, and get's the job done. You're probably asking, what even is Node.js and what's its purpose?

node.webp

In this article, we'll be going over the basics of Node and how to set up a development server on your local machine.

What is Node.js?

Node.js is an open-source, cross-platform, back-end JavaScript runtime environment that runs on the V8 engine and executes JavaScript code outside a web browser. Essentially, it allows you to run JavaScript on the server.

A common task for a web server can be to open a file on a server and return the content to the user.

PHP and ASP handle file requests in a different way than Node.js. Here's how:

PHP & ASP:

  1. First, the server sends the task to the computer's operating system.
  2. It then waits while the file system opens and reads the file.
  3. When the file is read, the server sends the file back to the client.
  4. It is now ready to handle the next request.

Node.js:

  1. First, the server sends the task to the computer's operating system.
  2. It's now ready to handle the next request.
  3. When the server receives a request, it reads the file and sends the file back to the client.

So, as you can see, Node.js eliminates the need for the server to wait for the file to be read. It is single-threaded, non-blocking, asynchronous programming, so it can handle multiple requests at the same time. Nice!!

What can Node.js actually do?

NodeJS can do a lot of things.

  • It can run JavaScript code on the server
  • it can generate dynamic web pages
  • It can create, open, read, write, and delete files
  • It can collect form data
  • it can add, delete, and modify data in your database

So what are we waiting for? Let's go ahead and get started!

Getting started

To get started with NodeJS, you need to install NodeJS. The official website has installation instructions for Node.js NodeJS.org.

The first thing you need to do is download NodeJS from the official website.

node.png

npm install -g npm

To see if you already have Node.js installed, you can check your version by running the following commands:

node -v
npm -v

Once you have Node.Js installed on your computer, we can now attempt to display a "Hello World" message in a web browser.

Create a file called index.js and paste the following code into it:

// Import the http module from Node.js
var http = require('http');
// Create a server
http.createServer(function (req, res) { 
  // Set the content type to be plain text
  res.writeHead(200, {'Content-Type': 'text/html'}); 
  // Send the response
  res.end('Hello World!');
// Listen on port 8080
}).listen(8080);

This code is basically telling the computer to write Hello World! to the screen. Now, if anyone goes to localhost:8080 in their web browser, they will see Hello World! on the screen.

Initiate the Node.js File

We can use the command line to run the file we just created. You can simply run the following command:

node index.js

Your computer now works as a real server! Anyone who tries to access your computer on port 8080 will see Hello World! on the screen.

Go ahead and start your internet browser, and type in localhost:8080 in the address bar. You should now see your development server running on port 8080.

localhost.png

Did you find this article valuable?

Support Anthony Smith by becoming a sponsor. Any amount is appreciated!