Programming With Jon Logo

Programming with Jon

What is Node.js

June 10th, 2020

[object Object]

Node.js, What is it?

I know that by now most developers have heard of and used Nodejs(node). I wanted to create a post that very quickly showed the power of using Node. We can create a very simple web server with node in under 15 lines of code. It won't be the most glorious of web servers but it will be a web server none the less.

Setting up the Environment

I will start this process in the usual way of walking you through the process of setting up your environment. For those of you that already have node installed you can skip to the next section. If you don't already have node installed, whether this is your first time with node or programming in general you will need a couple of things to get started.

Downloading Node

First you will need to go to nodejs.org to download a version of node. I would recommend using the LTS version. Which at the time of writing this post is version 12.18.0. You can of course download the current version which supports the latest features but is also known to be a bit buggy. I recommend the LTS version. Once the file has downloaded open the download and go through the install wizard.

VS Code

I recommend the VS Code editor if you don't already have an IDE or text editor of choice. VS Code has some really awesome features that allow you to start coding with node very quickly. I won't be covering them here as this is just meant to cover setting up a basic server with node and getting you up to speed with using node if you come from another backend language such as PHP or Python. You can install VS Code from https://code.visualstudio.com/download

Once the download has finished go ahead and go through the install wizard to finish setting it up. Make sure you check the add to path checkbox as it will be important in the next steps to come.

Creating Your First Node.js API

We will be creating our own web server. This will be very simple, you will be able to open your browser and see a message as well as see in your console/terminal window a message showing your server is up and running and on what port. Let's get things started.

server.js

As you can see above our file will be called server.js. Let's start by opening up our terminal and creating a directory to store our server. I am going to create my directory here: C:\Users\jon\source\repos\node-server. You can of course create yours anywhere that is easy for you to remember and access. Now that we have our directory setup lets go ahead and run this command from our terminal code . make sure that you are in the directory you want to store your files in. VS Code should now open inside the directory you just created and we are ready to roll.

Create the file server.js and type in the following code:


const http = require('http');

const port = process.env.PORT || 8000;

const server = http.createServer(function (req, res) {

res.end('Hello from Node.js!');

});

server.listen(port);

console.log(`Server listening on port ${port}...`);


That is all there is to it. Technically this server is less than 10 lines of code but I said 15 because with spacing to make it readable the code is about 10 or 11 lines.

We can run this file by typing node server.js from our terminal. If you are using VS Code then you can go to view -> terminal and use the integrated terminal built in to VS Code to run this command. Otherwise switch over to your terminal and type in the command above. If everything is successful you should see the message in your terminal saying Server listening on port 8000... Now go to your browser and type in localhost:8000 you should see your Hello from Node.js! message in the window.

A thorough view of the code

We can look over the code in this file and give a brief description of the content. First we have:

const http = require('http');

This line tells node to load up the core http module and store it in the http variable. The require() is a globally accessible function in Node.js and is always available. http is a core module which means that it is always available to be loaded via require(). Next we have:

const port = process.env.PORT || 8000;

This is where we choose our port for the web server. We store the port in the port variable. We can access process without using require() because process is a global object with information about the currently running process. The process object is always available when running Node. Next we have:

const server = http.createServer(function(req, res) {

res.end('Hello from Node.js!');

})

Here is where the actual server is being built. We use http.createServer() to create a HTTP server object and assign it to the server variable. It accepts a single argument: a request listener function. The request listener function will be called every time there is an HTTP request to our server. Basically every time you visit http://localhost:8000. The first argument in the function call, req, is a bit outside the scope of this post since we aren't using it. The second argument to our request listener function is the response object, res. We use this object to send data back to the browser. We are sending the string "Hello from Node.js!" as well as ending the connection with a single method call: res.end("Hello from Node.js!").

Our server object has been created. If a browser request comes in, our request listener function will run, and we will send data back to the browser. The only thing left is to allow our server object to listen for requests on a particular port.

server.listen(port);

And finally, we print a message for convenience, telling us that our server is running and which port it's listening on.

Conclusion

I hope that this post was helpful. I know that there are a ton of tutorials out there on Node and how it works I want to thank you for taking the time to read through this post and don't forget to check out my other posts. I will definitely be posting more about Node in the future. I also plan on doing a couple of posts on my thoughts about Deno and what I have learned about using it over Node. Stay tuned.

Created by Jonathan Reeves, © 2020