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: var, let, const

JavaScript provides three ways to declare variables: var, let, and const. They differ in terms of scope, reassignment rules, and usage.

var
  • An older way to declare variables (before ES6).
  • Has function scope: it is visible throughout the function, but does not respect block boundaries (e.g., if, for).
  • Allows variable redeclaration within the same scope.
var x = 10;
if (true) {
    var x = 20; // Same variable!
}
console.log(x); // Outputs 20
let
  • A modern way to declare variables (introduced in ES6).
  • Has block scope: visible only within the block where it is declared.
  • Does not allow variable redeclaration within the same block.
let y = 10;
if (true) {
    let y = 20; // Different variable!
    console.log(y); // Outputs 20
}
console.log(y); // Outputs 10
const
  • A modern way to declare constants (introduced in ES6).
  • Has block scope, similar to let.
  • Does not allow variable reassignment.
  • However, objects and arrays can still be modified.
const z = 10;
// z = 20; // Error: constants cannot be reassigned!

const arr = [1, 2, 3];
arr.push(4); // Allowed, modifying the contents
console.log(arr); // Outputs [1, 2, 3, 4]
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