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.
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?
- Text Editor
- 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.
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:
Nodejs will be installed in the directory
/usr/bin/
, so when using NPM the parameters -g
must 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:
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:
To stop the Ctrl+ press 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
http
from node_modeuls
.
What is it
node_modules
?node_modules
is a directory that stores the Nodejs library. Later we will have deeper language.
The library
http
functions to handle protocols http
such 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/html
. We 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:
The result will be like this:
Reference: https://www.petanikode.com/nodejs-pemula/
0 Komentar untuk "Learning Nodejs # 1: Introduction to Basic Nodejs (for Beginners)"
Silahkan berkomentar sesuai artikel