REST API with Express.js

Traditionally JavaScript has been used only in the front end, so the code was loaded to the browser and executed there. But in the late-2000s Node.js was created and that made possible to use it also in the back end.

In the examples of this tutorial there is no need to deeply learn JavaScript, so I will mention here only those things that might seem strange, if you haven't been studying JavaScript.

Variables in JavaScript

You can use keywords var, let and const, when you are introducing JS-variables. The difference between those is that:

  • var x=5; // the scope of x is global
  • let x=5; // the scope if x is inside the block
  • const x=5; the scope of x is global and you can not change the value


Callbacks are functions which are passed as an argument to some other function. You will need them when you are using some asynchronous functions.

Here is an example which demonstrates the callback system:


function doSomething(){
    console.log("Demonstrating the callbacks");
console.log("The application is started");
Here the function setTimeout has two parameters: function doSomething and delay. So, we will pass the function doSomething as an argument to the setTimeout function. So now the function doSomething is the callback.

And because setTimeout is asynchronous function the application is not blocked and thats why the console.log("The application is started") will appear first.

Anonymous functions

Quite often the previous example is made like this

    console.log("Demonstrating the callbacks");

console.log("The application is started");
So, now the callback function don't need a name and we can say that it is an anonymous function.

Arrow functions

Arrow functions makes the code shorter, but also harder to read (at least for starters).

We can replace the previous code with this

    console.log("Demonstrating the callbaks");

console.log("The application is started");

Here is a short article about callbacks

Toggle Menu