Node.js is a new JavaScript-based ecosystem
This is a server written in JavaScript, but it can be much more.
Node.js can run locally on Linux and Windows. It interacts with the browser, so you can create applications in JavaScript that run locally or remotely and that will use HTML 5 and Canvas as an interface. We will build on this opportunity and its implications.
Node.js is not really a replacement for Apache, but a tool for webmasters who want to completely control what happens on a remote server, or have some kind of local server for applications running simultaneously and sharing data. MySpace is an example of a site running Node.js with the Express framework. Groupon in 2013 moved its infrastructure from Rail to Node.js and became one of the largest sites to operate the system. Other major sites using Node include Paypal, Yahoo.
Node.js - Application Server
This is a server written in JavaScript that can be used as a module of a program written in any other language.
With the JIT V8 compiler, it can interpret JavaScript scripts. He himself interprets V8. Formally, this is a bookstore added to V8 and encapsulated in the launcher.
He reacts to events, everyone opens a stream. It works asynchronously, each event invokes a process that runs independently of the others. It also does not block, which means that you do not need to block something to prevent further access when performing an operation, as is the case in multithreading.
It can be installed on the server instead of Apache, where it will be given URLs of pages for downloading or running applications. They will be executed in the browser at the local post.
It can be installed locally, easier than Apache, it is an executable file that is enough to run on the command line to make it available to the browser with a local IP and port number.
It is less complete than Apache, sometimes you have to add code to handle the operations that Apache performs from the very beginning (for Node.js 6.0), or add a framework.
It is protocol independent, can use HTTP to provide HTML pages or other protocols for other types of applications.
The operation is described in the diagram below. A JavaScript script creates a server instance and assigns it a port through which a browser or other agent can interact and interact with the script.
Used node.js, giving it a JavaScript program to interpret
Suppose we create a script called monscart.js. We start the C&C server as follows:
node moncart.js
The script must contain code to create a server instance, for example:
var http = require('http'); function f(request, response) { response.writeHead(200, {'Content-Type': 'text/plain'}); response.write(new Date() +'\nServeur en ligne...'); response.end(); } var server = http.createServer(f); server.listen(1000);
The server is created as follows:
- Load the http module for the server following this protocol and assign it to a variable.
- Create an http server by calling the createServer method of the http module with the command: server.createServer ().
This method has as a parameter a function that is called by a server object with two objects (which are named request and response). - The writeHead method of the response object creates a header with an HTTP status code of 200, meaning OK.
- The write method displays a message.
- The end method marks the end of server send (no communication). It can also show the message as a record.
- The query object is not used here, but access to useful functions is provided. See the following example.
- Assigns a port with the created server list method server.listen (1000).
An example is number 1000, other values ranging from 0 to 65535 are possible, since this number is not used by another service.
The createServer parameter is a function that sends a message to a user. It can be declared an anonymous function, it is used as a parameter by an internal function for the server object and contains request and response objects that remain assigned after the f function is returned.
The code benefits from the closures inherent in JavaScript. Closing is to give global coverage to variables declared in a function, but making them available only to that function. When a function is called multiple times, the variables retain their value from one call to another. Thus, the process can be started in the sandbox, independently of the others.
Enter the browser URL to access the application:
127.0.0.1:1000
Corresponds to the local IP followed by the selected port number. You will see the date and time, and then the message "Online server...." Or:
localhost:1000
This URL remains available until you cancel the node monscart.js command or close the command prompt window. If you restart another script, for example, node script 2.js, then when you reload the page, you will have access to this new script.
Demonstration: Securing data
Script showing how data can be stored from one connection to another.
First, we will add the title of the previous scenario to support accents and other foreign codes in order to be able to integrate the text into our simplified interface:
response.writeHead(200, {'Content-Type': 'text/plain;charset=utf-8'});
UTF-8 encoding supports most characters.
We will add a connection counter and show a counter, so you can make sure that the data created during one connection is saved for the next, which is necessary to share data between applications.
To test the example, load the page into the browser several times with the previously specified URL .
var http = require('http'); var counter = 0; function f(req, response) { if(request.url == '/favicon.ico') return; response.writeHead(200, {'Content-Type': 'text/plain;charset=utf-8'}); if(counter == 0) response.write(new Date() +'\nServeur en ligne...\nPremière connexion.'); else response.write((counter + 1) + " ème connexion."); counter++; response.end(); } var server = http.createServer(f); server.listen(1000);
Counter is a global variable. When you log in for the first time, only the login number is displayed.
First line...
if(request.url == '/favicon.ico') return;
... is used to remove the side effect: that the browser, when loading the page, makes two consecutive requests: one for the page, the other for the favicon, which increases the counter twice!
You can access the URL from multiple browsers while saving your data.
The server can be accessed without a browser. For example, locally, with a script in PHP, you can use curl to interact with the server, simultaneously with the browser. This will be discussed in another article.
As long as the server remains active (when it is online, it must remain constantly), you can communicate with various browsers or other types of user agents.
Next step: Create a page or application server using Node.js.
Get source code
- The monscart.js and persistance.js files correspond to the first and second scenarios, respectively .
Useful modules
With npm included in the archive, you can automatically add modules to shut down the server.
- Step. Allows you to arrange a set of actions either sequentially or in parallel. This allows you to better understand the asynchronous operation of the system .
- Asynk. To monitor parallel or sequential processing.
- Express. Application infrastructure.
Resources and Information
- Sandbox for code verification.
- Node.green. Real-time state (almost) of the ECMAScript implementation for each Node version.
- Knot/Chakra. Microsoft's version of Node, where the V8 is replaced by Chackra, for Windows 10.