NodeJS Basics

Mokhtar Ali
3 min readNov 18, 2020

Hi! I am a flatiron Bootcamp graduate, and on my job search journey, I found that so many jobs require knowledge about NodeJS. While learning about NodeJS reinforced my knowledge about Backend Development, it helped me have a better understanding of how servers run.

First a little refresher on how the web works:

The client visits a webpage, entering a URL, the server lookup the domain or IP address. The browser sends an HTTP which stands for (HyperText Transfer Protocol) request to the server that belongs to the domain. This is when the code you write on your server using NodeJS or Python or Ruby on Rails responds back to the browser before it goes to the database.

What is NodeJS?

NodeJS is a Javascript runtime that allows you to run Javascript code on the server.

How to install NodeJS?

First check your terminal by writing this command: node -v If you get an error, visit Nodejs.org and download the latest version for your system.

How to use NodeJS?

You can run NodeJS on any machine or system by running node on your terminal. Also, you can download some development environments such as VSCode.

First App using NodeJS:

console.log(‘Hello World’)

To execute this code, got to the terminal on VSCode and run

node app.js

Why is it important to learn NodeJS?

  • NodeJS is a great tool for writing Utility Scripts and Building Tools. If you used React or Angular, you have already used NodeJS indirectly a lot for all the built processes for what these frameworks needed.
  • NodeJS you don’t just run a code that runs on your server, you actually create the server itself.

Alternatives to NodeJS:

  • Ruby on Rails
  • PHP
  • Python

Now let’s create a server using NodeJS:

  • Let’s create an App.js file
const http = require('http');const server = http.createServer((req, res) => {
console.log(req)
})
server.listen(3000)
  • First step we required or imported “http” a core model that launches a server and sends requests.
  • Then we used the createServer method that comes from the http core model. The createServer method takes two arguments: req which is a shortcut to request, and res a shortcut for a response. Using ES6 syntax we use the arrow function using the equal to and greater signs.
  • The createServer method returns a server. And we can call the .listen method that takes the server’s name as an argument. For now, we use the local port 3000.

Explaining more what happens behind the scene:

  • We executed node app.js .
  • Started Script by parsing the code, registering variables and functions.
  • Then we went into Event Loop which goes running for incoming request listeners.

Routing in Node:

const http = require('http');
const server = http.createServer((req, res) => {
const url = req.url
const method = req.method
if(url === '/'){
res.write('<html>')
res.write('<head><title>First Node Page</title></head>')
res.write('<body><h1>Hello World</h1></body>')
res.write('</html>')
return res.end()
})
server.listen(3000)

FS package:

fs module is used to access the physical file system. The fs module is responsible for all the asynchronous or synchronous file I/O operations. and we require it by:

const fs = require('fs')

In the next blog, I will dive more into how to create an Application using the ExpressJS library.

--

--

Mokhtar Ali

Web Developer | World Traveler | Positive Attitude