Shopware Silver & extension partner
Certified Advanced Developers & solution architects
200+ E-commerce Rrojects
50+ Professional developers
Every Request consists of Asynchronous and Synchronous Execution.
Node.js is the JavaScript runtime environment that is based on Google’s V8 Engine with the help of Node.js we can run the JavaScript outside of the browser. Other things that you may or may not have read about Node.js is that it is single-threaded, based on event-driven architecture, and non-blocking based on the I/O model.
Node ensures that these Async Tasks are Event Based and Asynchronous. Creating Event Based architecture ensures that the Main Thread is not waiting for the Asynchronous Request to complete. The main concepts that are involved in Node Processing are: • Node.js is a Single Threaded Event Loop • Node has Non-Blocking I/O Model • Event-Based, Instead of Waiting for I/O Operation
It is often Mistaken that Node is Multi-Threaded. It is based on Single Threaded Event Loop Architecture. This Event Loop is Single Threaded. All the Requests are received on this Event Thread. Every Request consists of Asynchronous and Synchronous Execution. The Main Thread is responsible to Execute Only the Synchronous Part of the Request. As soon as Asynchronous I/O Operation is encountered, the Main Thread allocates a Background thread to process the I/O Operation.
Node’s Main Thread does not Wait for External Time-Consuming process to complete. While the Background Thread is Executing the I/O Function, the Main Thread does not wait for this task to complete. While the Async Task is executed in the Background, the Main Thread is free to take other Requests. Once the Execution of the Background Thread is complete, it returns to the Main Thread for Further Execution. Node’s Main Thread is continuously switching between different Requests to execute its Synchronous Part.
The Background Thread uses an Event-Based Approach to Notify Main Thread. Each Async Task consists of some Callback Function associated with it, once the Async Task is Complete, the Background Thread raises an Event to Notify the Main Thread about the completion of the Async Task. The main Thread might be busy processing some Other Request. Meanwhile, the Request waits on the Main Thread, and as soon as Main Thread is free, it takes up the Callback Request for Further Execution.
As you can see at the bottom side the figure below, Node.js has a large range of functionality in its library. The HTTP module we used in the sample code earlier contains both client and server classes, as you can see at the right side of the figure. The HTTPS server functionality using TLS or SSL lives in a separate module.
The following example creates a web server that listens for any kind of HTTP request on the URL http://127.0.0.1:5000/ — when a request is received, the script will respond with the string: “Hello World”.
If you have already installed the node, you can follow these steps to try out the example:
1.Open Terminal (on Windows, open the command line utility)
2.Create the folder where you want to save the program, for example, node-server, and then enter it by entering the following command into your terminal:
Code: cd node-server
Using your favorite text editor, create a file called hello.js and paste the following code into it:
// Load HTTP module
const http = require("http");
const hostname = "127.0.0.1";
const port = 5000;
// Create HTTP server
const server = http.createServer(function(req, res) {
// Set the response HTTP header with HTTP status and Content type
res.writeHead(200, {'Content-Type': 'text/plain'});
// Send the response body "Hello World"
res.end('Hello World\n');
});
// Prints a log once the server starts listening
server.listen(port, hostname, function() {
console.log(`Server running at http://${hostname}:${port}/`);
})
2. Save the file in the folder you created above.
3. Go back to the terminal and type the following command:
Code: node hello.js
Finally, navigate to http://localhost:5000 in your web browser; you should see the text “Hello World” in the upper left of an otherwise empty web page.
If you want to build your own web shop or have questions relating to e-commerce development platforms or apps, read our blog posts for Shopify and Shopware. Our dedicated outsourcing team can be your collaborator in e-commerce.