Blog for Learning

| lesson material | material summary | questions and answers | definitions | types and examples | other information | materi pelajaran | ringkasan materi | pertanyaan dan jawaban | definisi | jenis-jenis dan contoh-contoh | informasi lainnya |

Powered by Blogger.

Pages

Learning Nodejs # 1: Introduction to Basic Nodejs (for Beginners)

Beginner Nodejs
Since its presence, Nodejs has brought a lot of changes to the world of programming, especially the web.
With Nodejs, we can create Web, Desktop, Mobile, and even IoT (Internet of Things) applications .
... and the cool thing is, Javascript is the most popular language — on Github — today.
The most popular programming language on Github
In 2016 and 2017, Javascript still holds the # 1 most used language on Github.
All this because of nodejs. ðŸ˜„
The javascript ecosystem has changed a lot and evolved.
What is Nodejs?
Why do you have to learn Nodejs?
How can I start learning Nodejs?
We will soon discuss more deeply in this paper.
Starting from the history of the origins of Nodejs ...
... to make a simple web application with Nodejs.
Ready?
Let's start…

Introduction to Nodejs and their History

Initially Javascript was only used to create web pages.
But now, thanks to Nodejs ... we can make desktop, web, mobile, CLI, IoT, etc. applications.
Is Nodejs a new programming language?
No, Nodejs is not a programming language.
Then what?
Framework?
For more details, it's good that we listen to the history.
In 2009, a programmer named Ryan Dahl created Nodejs.
The Javascript Programming Language can only initially run on top of the browser, because there is a runtime engine in it.
Then, came the idea:
"What if the engine that is in the browser, we remove it so that we can execute Javasript outside the browser"
So Nodejs was born.
So…
Nodejs is a platform for executing Javascript programs outside the browser.
Nodejs uses a runtime engine called V8, which is a javascript runtime enginefrom Google Chrome.

Preparing Tools for Learning Nodejs

What equipment must be prepared to study Nodejs?
  1. Text Editor
  2. Nodejs
That is all…
Yes, that's all.
Let's install it.

Installing Nodejs on Linux

Please type the following command to install nodejs version 6:
curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -
sudo apt-get install -y nodejs
As for version 8:
curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
sudo apt-get install -y nodejs
For version 10:
curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
sudo apt-get install -y nodejs
For other distributions and operating systems, please check here: https://nodejs.org/en/download/package-manager/
There are many versions of Nodejs, which one should I install?
When this article was written, the version that is widely used in the production environment is version 6.
By 2019 it may change.
LTS version of Nodejs
Even version of Nodejs has a longer support period called LTS (Long Term Support) .
Currently the circulating versions of LTS are versions 6 and 8.
What's the difference between version 6 and 8?
The difference is version 8 has more features and is updated compared to version 6.
I recommend installing version 6 first. Because of our need to study only.
But if you want to install the newer version of Ang, it doesn't matter.
Once installed, try checking the version with the command:
node --version
npm --vserion
On my computer:
Version nodejs and npm
Nodejs will be installed in the directory /usr/bin/, so when using NPM the parameters -gmust be accompaniedsudo

Creating a Text Based Program with Nodejs

The next thing to do is create a program Hello World!.
Please open the text editor and write the following code:
console.log("Hello World!");
Save by name hello-world.js.
After that, execute with the command:
node hello-world.js
Then the result:
Nodejs execution
Cool, our Javascript program can run on the CLI.

Make a Webserver with Nodejs

Let's try making a webserver with Nodejs.
Please type the following code:
var http = require('http');

var server = http.createServer(function (req, res) {
    res.end("Hi, selamat datang di nodejs");
});

server.listen(8000);

console.log("server running on http://localhost:8000");
Save by name server.js, then execute:
node server.js
The result:
Server Execution
Now open the browser and navigate to the address http: // localhost: 8000 , the result:
Results of the webserver nodejs
To stop the Ctrlpress server c.
It's easy, right…
Please explain the meaning of the code above.
Okay, I'll explain.
The first starts from:
var http = require('http');
The function of the code is to import the library httpfrom node_modeuls.
What is it node_modules?
node_modulesis a directory that stores the Nodejs library. Later we will have deeper language.
The library httpfunctions to handle protocols httpsuch as making and receiving requests .
Then we create a server object with the following code:
var server = http.createServer(function (req, res) {
    res.end("Hi, selamat datang di nodejs");
});
The server that we created, will respond "Hi, selamat datang di nodejs"when accessed by the client .
Finally, we specify the port number that will be used by the server:
server.listen(8000);

Make a Webserver with an HTML Response

The web server that we created earlier ... only responds in plain text.
What if you want to respond in HTML?
We must modify the header .
The code example is like this:
var http = require('http');
http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/html'});
    res.write('Hello <b>World</b>!');
    res.end();
}).listen(8000);

console.log("server running on http://localhost:8000");
The function of res.writeHead(200, {'Content-Type': 'text/html'});is to determine the response header .
In the above code, we determine the type of response in the form of text/htmlWe can also specify other types such as JSON, PDF, XML, and others.
Then res.write('Hello <b>World</b>!');its function is to write response body or content that will be sent to the client .
Finally end the response with res.end().
So when we execute it:
Execute the webserver nodejs
The result will be like this:
Execute the webserver nodejs

0 Komentar untuk "Learning Nodejs # 1: Introduction to Basic Nodejs (for Beginners)"

Silahkan berkomentar sesuai artikel

 
Template By Kunci Dunia
Back To Top