I hope you guys enjoyed my last post on transparent simple html form. In this post we will talk about one of the most popular backend server environment.
What is Node.js?
Node.js is an open-source, cross-platform, JavaScript runtime environment that executes JavaScript code outside of a web browser.
Node.js runs single-threaded, non-blocking, asynchronously programming, which is very memory efficient.
Use Cases of Nodejs:
- Real-Time Apps
- Data-Intensive Apps
- To build Scalable Backend
- Non-Blocking Asynchronous Thread
Getting Started
Now after downloading Nodejs on the computer let's try to create hello world program. Remember, Node.js files must be initiated in the "Command Line Interface" program of your computer. For Windows users, press the start button and look for "Command Prompt", or simply write "cmd" in the search field.
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('Hello World!');
}).listen(8080);
You can choose any port available on computer to listen.
Now start your command line interface, write node myfirst.js and hit enter:
C:\Users\User Name>node myfirst.js
Now let's try to understand the terms in our first program.
Modules(require):
A set of functions you want to include in your application.
Node.js has a set of built-in modules which you can use without any further installation.
To include a module, use the require() function with the name of the module.
var http = require('http');
The first argument of the
res.writeHead()
method is the status code, 200 means that all is OK, the second argument is an object containing the response headers.
Now your application has access to the HTTP module, and is able to create a server.
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('Hello World!');
}).listen(8080);
So this was the introduction to Nodejs using simple Hello World program.
Most of the contents pf this post are from w3schools.com so for any elaborated information you can https://www.w3schools.com/nodejs/nodejs_intro.asp refer this page.
I hope it will help you to quickly get started learning Node.js.
Happy Coding!!...
Top comments (0)