A Rookie’s introduction to Node.js

Emmanuel Akhigbe
Webtips
Published in
4 min readJun 13, 2019
NodeJs

Node js is a Javascript runtime environment built on chrome’s V8 engine.

Now don’t be confused, I’ll explain. To understand this definition, we’ll need to know; what Runtime means and what Chrome’s V8 engine is.

Runtime is the instance of time where the code you wrote is being executed.

Chrome’s V8 engine is a Javascript engine written in C++ that runs and executes your javascript code.

With this knowledge, you should be able to understand the definition of Node js above.

Why Node js?

  1. It is Asynchronous
  2. It is non-blocking
  3. It is super fast

What do I mean by asynchronous? First, let us understand it’s opposite; synchronous, a process is synchronous if it’s tasks are executed in a sequence ‘one after the other’ this is orderly right? What is wrong with this? Say you go to a restaurant to buy a piece of sweet, and you meet Mr. A at the counter checking out a months worth of grocery, a synchronous process means you’ll have to wait till the Mr. A is done before you can pay for your sweet, that’s enough to discourage you from getting the sweet isn’t it? Well, an asynchronous process will push Mr. A’s time-consuming check out to the background and attend to you, which process is better? Right, the Asynchronous process. Because Node js is Asynchronous by default, it serves you faster.

If you understand my explanation above, you’ll understand what I mean by nonblocking;

Hint: the heavy task doesn’t stop the other tasks from executing.

What is npm?

npm stands for node package manager, it contains awesome 3rd party modules that you can install and use in your javascript code.

So let’s get our hand dirty, we’ll be building a pretty simple server.

First things first

  1. We need to install node from https://nodejs.org/en/ I’d advise that you install the ‘Recommended For Most Users’ version of node. After downloading Node Js, open the file and follow the installation instructions in the wizard.
  2. Confirm that node has been successfully installed; open your terminal or command prompt and type in ‘ node -v’, and press enter, the version of node should show.
  3. Create a folder and name it myFirstServer (Your choice), open the folder in your text editor, and create a JS file in it.
Yours should look like this

Now we can start.

Type in this code in your app.js file

Line 1: it uses require() to include the HTTP module into your server, the HTTP module will allow Node.js to transfer data over the HyperText Transfer Protocol (HTTP).

Line 3: This specifies the port you want your server to run on in your machine.

Line 5: The http.createServer() method basically creates an HTTP Server object, it accepts a call back function as an argument, the call back function has 2 arguments; req and res which mean request and response respectively, basically, req contains what the client is asking your server, and res will contain the response you’ll give the client.

Line 6: Status codes are numbers that tell you the state of your response; 200 means everything went well, 404 means what was requested could not be found, 500 means the server is having some experiencing a downtime e.t.c, So res.statusCode is setting the status code to 200.

Line 7: Content-Type is the format/type of the response data, in this case, we are responding in plain text hence, we set the Content-Type to ‘text/plain’, if we were going to respond in JSON which is the likely response type in real-world applications we would set the Content-Type to ‘application/json’.

Line 8: res.end runs at the end of our response. Here we are telling the server to output ‘Hello World’ when the response has ended.

Line 11: server. listen() tells the server what port to listen to remember we specified this port on line 3, server.listen() accepts 1 mandatory argument which is the port to listen to, it also accepts a callback function that runs when the server starts listening on that port ( we use this to know that the server has started listening).

Start the server

Open your command prompt or terminal and navigate to the Js file you wrote created your server.

Type in ‘ node <name of the file>’, since mine is app.js, to start the server; I will type in ‘node app’. If everything is fine, you will see ‘Server running…’.

Open your browser and navigate to ‘localhost:<port specified in line 3>’, since I set mine as 3000, I will enter ‘localhost:3000’ press enter and you should see ‘Hello World’

Hello World

Congratulations, you have built your first server! Here is are some awesome resources you can check out

https://www.youtube.com/watch?v=TlB_eWDSMt4
https://www.youtube.com/watch?v=RLtyhwFtXQA
https://www.youtube.com/watch?v=fBNz5xF-Kx4

--

--

Emmanuel Akhigbe
Webtips
Writer for

Full stack web developer and designer. I love learning, and writing code to solve problems. Twitter: @theoscoder