Monday, June 03, 2024

Node.js from Scratch, Getting Started with Backend Development

What is Node.js? Before we answer this question, it’s better to first clarify what it is not. Node.js is not an enhanced JavaScript programming language, nor is it a framework for JavaScript developers. In short, Node.js is a platform for developing web applications, application servers, any type of network server or client, and a general-purpose development environment based on JavaScript that executes on the server side and is built on asynchronous events. When we say platform, we mean much more than a platform; we mean cross-platform, which has no equivalent translation for many foreign languages, but it means that Node.js works on different operating systems like Windows, MacOS, Linux, and others.

It is an open-source platform, of course, free under the MIT license. It is a platform that is not a framework; frameworks are just a part of this platform, like Express.js. With Node.js, you create highly scalable and highly performant web applications. It is a wrapper around the Chrome V8 browser engine. With Node.js, you can easily create desktop GUI applications in JavaScript. Visual Studio Code, Atom, Postman, etc., are based on Node.js. It is also very popular for developing micro servers and in the world of IoT - Internet of Things. It is simply incredible how powerful this platform is, how it simplifies complexity, and what you can do with it; we simply don’t know how to explain all this in brief to a programming beginner.

Node.js is otherwise intended for experienced programmers who are proficient in coding applications, developing web applications, and knowing the JavaScript programming language. But there is no need for this to discourage you; you will just have to roll up your sleeves much more than when you are learning the JavaScript programming language. Even if you know JavaScript, it doesn’t mean you can fully use all JavaScript commands in Node.js. For example, this simply won't work in Node.js:

document.getElementById('');

Simply put, HTML DOM is not built into Node.js. You cannot use the document or window objects. Instead of this line of code, you would use Puppeteer, a Node.js library that provides a high-level API. But, not to complicate things now, Node.js is also not a new programming language; it uses the JavaScript programming language but does some things differently.

Node.js Application

 Node.js Application

If we go back in time, to 1995, a new programming language called LiveScript appeared under the code name Mocha. But due to the popularity of the Java programming language, it was renamed JavaScript. On May 27, 2009, Node.js was released. The Java programming language has the motto "Write once, run anywhere!" And now, Node.js has the motto "JavaScript for everything!" It's clear that both programming languages tend to be used everywhere. Node.js made it possible for JavaScript to overcome some limitations of the Java programming language in such a way that you can use the Java programming language within Node.js.

Do you now understand the capabilities of Node.js we are talking about here? As early as 2009, Node.js literally gave wings to the JavaScript programming language, allowing JavaScript to be executed on the server side and outside the scope of any browser, which is not the case with the JavaScript programming language itself. Node.js is an extremely popular choice for creating and developing command-line tools used in software development. Babel is often used to transpire modern ES-2016 code to run old code for viewing on older browsers. Node.js allows you to easily test web user interfaces.

The Puppeteer library provides control over a headless instance of the Chrome web browser. Electron and NW.js are frameworks for developing desktop applications on Windows, MacOS, and Linux operating systems. Applications use HTML5, CSS3, JavaScript, and frameworks like Bootstrap, React, Vue.js, or AngularJS. With Node.js, you can also create mobile applications. But note that Apple's App Store rules do not allow JIT capabilities. Regarding IoT - Internet of Things; Node.js runs on most computers that support ARM.

What Makes Node.js So Special: Benefits That Can Change Your Approach to Programming

Node.js simply has a system that uses a single programming thread. This means that the server does not have the complexity of multithreaded systems. In simple terms, Node.js addresses complexity. When an asynchronous function is called, control quickly passes to the event loop instead of causing the runtime environment to block. If we use programming terminology to say that Node.js juggles, it means that thanks to asynchronous requests, Node.js performs multiple tasks simultaneously. The result of all this is that when you create a Node.js application compared to others:

  • The application is created twice as fast with much fewer people.
  • The application has a third less code.
  • The application has up to 40% fewer files.
  • The application handles requests per second twice as fast.
  • The application has 35% faster response times.

Add to all this the adoption and popularity of the JavaScript programming language, high scalability, and agile development, and you have every reason to learn and use Node.js.

Getting Started with Node.js: From Installation to Your First Application

To start learning or doing a project in Node.js, you first need to go to the Node.js official site here and install the Node.js platform on your operating system. How it is done, you can watch the following video.


Windows - 13. How to install Node.js?

However, if you are using a Linux operating system, you can watch the following video.


Linux - 14. How to install Node.js?

Another not a little naïve thing is the choice of IDE - Integrated Development Environment. As for us, we will use Visual Studio Code as Node.js developers do, although the choice is yours. See how to install Visual Studio Code on a Windows operating system in the following video.

Windows - 1. How to install Visual Studio Code & .Net Core?

If you use the Linux operating system, watch the following video.


Linux - 2. How to install Visual Studio Code and .Net Core?

If you have installed Visual Studio Code, it is necessary to add some extensions that you saw in the video. So, let's see what they are for:

  • npm Intellisense – When you use `require` to import modules, you get autocomplete functionality.
  • ESLint – Checks and finds syntax errors as you type, making it easier to spot them.
  • Prettier – Code formatter – Makes your code look nicer.
  • GitLens – Git supercharged – Helps you better understand your code.
  • JavaScript (ES6) code snippets – Eases typing, controls JavaScript syntax, and also TypeScript.
  • DotENV – Helps you with .env files.
  • Better Comment – Makes your comments look more professional.
  • YAML – Sometimes you will work with .yml files, it helps you keep them valid.
  • Code Spell Checker – Incorrect spelling in English causes errors, these resolves spelling issues.
  • Vscode-icons – We want a better, more pleasant, and well-integrated development environment.

Of course, depending on what you use Node.js for, you can add other extensions later. Keep in mind not to overdo it. If an extension causes errors, simply uninstall it.

First Program in Node.js: How to Create a Hello World Application

If you have done everything before instead of one line of code to say Hello World in the terminal; see how to create a server that says Hello World. Open the directory nodejs_tutorial but previously create it via the Open dialog wherever you want on your computer. Then add a new lesson1.js file. That it is a JavaScript file. Just type the following code into it:

const http = require('http');

const hostname = '127.0.0.1';

const port = 3000;

const server = http.createServer((reqres=> {

    res.statusCode = 200;

    res.setHeader('Content-Type''text/plain');

    res.end('Hello World');

});

server.listen(porthostname, () => {

    console.log(`Server running at http://${hostname}:${port}/`);

});

If you have done everything previously instead of one line of code to say Open a terminal in Visual Studio Code and type:

node lesson1.js

The terminal will print:

Server running at http://127.0.0.1:3000/

Press and hold CTRL and click the link. Your browser will open and you will see the result:

Hello World

 

 

 

 

 

 

 

No comments:

Post a Comment