REST API with Express.js
JavaScript

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 backend.

This is not a JavaScript tutorial, so I will introduce only those things which are the most relevant in order to be able to understand the REST API examples.

To make REST API with JavaScript, you need to learn how to use Node.js and here is one tutorial for that https://www.w3schools.com/nodejs/default.asp.

Variables

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 of x is inside the block
  • const x=5; the scope of x is global and you can not change the value

Assosiative array

If you know how to handle assosiative arrays, you can easily handle the data which you will get from the database.

Assosiative array is an array where the fields have names. Here is an example of that

const bookData=[
    {'name' :"C++", 'author' :"Jim Smith"},
    {'name' :"Java", 'author' :"Lisa Jones"},
    {'name' :"MySQL", 'author' :"Bob Danieles"}
];
You can print all books from the array like this
console.log(bookData);
And you can print the first row from the array like this
console.log(bookData[0]);

Objects

Here is an example about using objects in JavaScript

//person is an object
const person={
    fname:'Teppo',
    lname:'Testi',
    showData: function(){
        console.log(this.fname+ " "+this.lname);
    }
};
//calling the showData method of the person object 
person.showData();
And another example where the object includes assosiative array
const person={
    personData:[
        {'fn' :"Jim", 'ln' :"Smith"},
        {'fn' :"Lisa", 'ln' :"Smith"},
        {'fn' :"Ann", 'ln' :"Jones"},
    ],
    showData: function(){
        console.log(this.personData);
    },
    isertData: function(addFn, addLn){
        this.personData.push({fn:addFn, ln:addLn});
    }
};

person.isertData('Teppo','Testi');
person.showData();

Callbacks

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:

setTimeout(doSomething,2000);

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

setTimeout(function(){
    console.log("Demonstrating the callbacks");
},2000);


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

setTimeout(()=>{
    console.log("Demonstrating the callbaks");
},2000);


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

Here is a short article about callbacks https://www.freecodecamp.org/news/javascript-callback-functions-what-are-callbacks-in-js-and-how-to-use-them/



Toggle Menu