Node.js isn’t a programming language, it isn’t web server, and it isn’t a framework. So, we can state as a Node.js Introduction, that is an application container that executes Javascript code in asynchronous ways. It interprets JavaScript using Google’s V8 JavaScript engine.
More best practices of the industry here Cracking WordPress for Security
Node.js Introduction
Node.js is used for creating a complex diversity of applications; like mobile, IoT, web, and robots. The project is jointly governed by a Technical Steering Committee (TSC), which is responsible for the high-level guidance of the project.
Github and Collaborators
The node.js/node GitHub repository is maintained by the TSC and additional Collaborators who are added by the TSC on an ongoing basis.
Installation
To install it, you need open in your browser the official node.js webpage and follow the installation instructions for your operating system.
First Steps
At first, you need to check the correct installation.
Open a terminal in your computer (command line prompt), and type:
$ node -v
If you don’t see the version number, please check and review the installation steps.
Also, you can type “node” and then you enter the REPL (Read-Eval-Print-Loop) interface. Next, test write “2+2” and press enter.
$ node > 2 + 2 4 > 1 + ( 2 * 3 ) - 4 3 >
NPM, the official Node Package Manager
Node owes a big part of its success to NPM, the package manager that comes bundled with it.
By default, NPM installs any dependency in the local mode. Here local mode refers to the package installation in node_modules directory. Globally installed packages/dependencies are stored in the system directory.
You can install local modules using:
$ npm install module_name
And if you want to install globally, simply add “-g” parameter.
To list installed local modules:
$ npm ls
And global modules:
$ npm ls -g
Other parameters that you can use with npm are: uninstall, update, search, publish, etc.
Comparison with PHP
For example, here is the code for a simple Node.js application:
var i, a, b, c, max; max = 1000000000; var d = Date.now(); for (i = 0; i < max; i++) { a = 1234 + 5678 + i; b = 1234 * 5678 + i; c = 1234 / 2 + i; }
console.log(Date.now() - d);
And here it is the equivalent written in PHP:
$a = null; $b = null; $c = null; $i = null; $max = 1000000000; $start = microtime(true); for ($i = 0; $i < $max; $i++) { $a = 1234 + 5678 + $i; $b = 1234 * 5678 + $i; $c = 1234 / 2 + $i; }
var_dump(microtime(true) - $start);
Now let’s look at the benchmark numbers. The following table lists the response times, in milliseconds, for these two simple applications:
Number of iterations |
Node.js |
PHP |
100
|
2.00
|
0.14
|
10'000
|
3.00
|
10.53
|
1'000'000
|
15.00
|
1119.24
|
10'000'000
|
143.00
|
10621.46
|
1'000'000'000
|
11118.00
|
1036272.19
|
This comparison is from site: tutsplus.com
package.json
The file package.json is in the root directory of any Node application and module and is used to define the properties of a package.
Example :
{ "name": "application_name", "version": "0.0.1", "private": true, "dependencies": { "express": "4.0.0" } }
Modules
Node.js uses a module architecture to simplify the creation of complex applications.
Each module contains a set of functions related to the “subject” of the module. For example, the HTTP module contains functions specific to HTTP.
In code:
var http = require('http');
Also, you can use your module:
var mymodule = require('./my-own-module.js');
The Hello World App
We can test node.js creating the classic hello world app.
Create a file hello.js, and type the following code:
console.log('Hello World!');
Now execute the code with this command:
$ node hello.js
You could see “Hello World!” in your console.
HTTP Server
This is an example of HTTP server:
testserver.js // load http module var http = require("http");
// create a server var server = http.createServer(function (req, res) { res.writeHead(200, {"Content-Type": "text/html"}); res.end("<h1>Hello World!</h1>"); }); // port to listen requests
server.listen(8000); // logging console.log("Waiting for requests");
To start server run:
$ node testserver.js
And to test in the browser, you can access http://localhost:8000 and you will see: “Hello, World!”
Express.js
Express.js is the most popular Node.js framework. It’s free and open source. It’s designed to make web and API applications in a flexible way.
Installation
Follow this link to carry out the installation of Node.js.
First Application
You can follow these steps to create express example application
$ express node_app $ cd node_app && npm install $ node app.js
Then enter in your browser to http://localhost:3000
Keep learning of latest industry trends on Introduction to Microservices Architecture