Evolving from Granular Form in Node

Debugging Node.js with the built-in debugger .

Like any good tool, the Node is well suited for certain use cases. For example, Rails, the popular web framework, it’s great to model complex business logic, eg. using code to represent life in a objectified plan that we live physically as accounts, loans, itineraries and inventories. Although technically you can do the same using the Node, there would be clear disadvantages knowing that the Node is designed to solve problems of I / O and not know about the part of ‘business logic’. Each tool has their focus to solve different problems. We hope this guide will help you to gain an intuitive understanding of the strengths of Node and you will know when it will be useful.

What is outside the scope Node?

Fundamentally the Node is only used as a tool to manage I / O around the files system and networking, it leaves other most beautiful features with third-party modules. These are some of the things outside the scope Node:
web frameworks

There are a good amount of web frameworks built on top of the Node (framework is a package that tries to solve a high level of problem and similar problems to model the business logic), but the Node is not a framework for web. Web frameworks are written to be used in Node and do not always make the same kind of decisions about adding complexity, abstraction and understanding that the Node does and may have other priorities.

Language syntax

The Node uses JavaScript and does not change anything about it. Felix Geisendörfer has a beautiful content written about the ‘writing style Node’ here.
Abstraction language

Where possible the Node will use the simplest way to write something. more ‘beautiful’ code makes your JavaScript more complex and committed with advantages and disadvantages. Programming is hard, especially in JS where you have 1000 solutions to the same problem! This is the main reason for the Node opt for simplicity whenever possible and that can be a universal option. If you are solving a complex problem and you are dissatisfied with the way the Node implements things with original solutions JS, feel free to work it into your app or module using any abstractions you prefer.

A great example of how the Node uses callbacks. Early on it was experienced feature called ‘promises’ that adds some features to make asynchronous code look more linear. He was taken out of the Node core for a few reasons:

they are more complex than callbacks
it can be implemented in userland ( distriuĂ­do in npm as third-party module)

Considering one of the most universal and basic ideas that Node is : read a file . When you read a file and need to know where the errors occur , for example when your hard drive dies in the middle of reading . If you have promises all you have is a code like this :

fs.readFile ( ‘ movie.mp4 ‘ )
.then (function ( data) {
// Do something with the data
} )
.error (function ( error ) {
// Handle the error
} )

This adds complexity , unnecessary. Instead of two separate functions Node uses only a single callback function . Here are the rules:

When no errors pass null as the first argument
When there is error , pass it as the first argument
The remaining arguments are used for anything (usually data or responses because most of the time the node is reading or writing things)

Therefore, the Node uses the callback style:

fs.readFile ( ‘ movie.mp4 ‘, function (err , data) {
// Handle the error, and do something with the data
} )

LEAVE A REPLY

Please enter your comment!
Please enter your name here