What is Node.js.
Node.js is an open-source and cross-platform JavaScript runtime environment. It is a popular tool for almost any kind of project!
Node.js runs the V8 JavaScript engine, the core of Google Chrome, outside of the browser. This allows Node.js to be very performant.
A Node.js app runs in a single process, without creating a new thread for every request. Node.js provides a set of asynchronous I/O primitives in its standard library that prevent JavaScript code from blocking and generally, libraries in Node.js are written using non-blocking paradigms, making blocking behavior the exception rather than the norm.
Why use Node.js
When Node.js performs an I/O operation, like reading from the network, accessing a database or the file system, instead of blocking the thread and wasting CPU cycles waiting, Node.js will resume the operations when the response comes back. This allows Node.js to handle thousands of concurrent connections with a single server without introducing the burden of managing thread concurrency, which could be a significant source of bugs. Node.js has a unique advantage because millions of frontend developers that write JavaScript for the browser are now able to write the server-side code in addition to the client-side code without the need to learn a completely different language. In Node.js the new ECMAScript standards can be used without problems, as you don't have to wait for all your users to update their browsers - you are in charge of deciding which ECMAScript version to use by changing the Node.js version, and you can also enable specific experimental features by running Node.js with flags.
History of Node.js
Node. js was written initially by Ryan Dahl in 2009, about thirteen years after the introduction of the first server-side JavaScript environment, Netscape's LiveWire Pro Web. The initial release supported only Linux and Mac OS X. Its development and maintenance was led by Dahl and later sponsored by Joyent.
Node.js happened to be built in the right place and right time, but luck isn't the only reason why it is popular today. It introduces a lot of innovative thinking and approaches for JavaScript server-side development that have already helped many developers.
The journey so far.
2009
The beginning of Node.js
Npm was created
2010
The beginning of express.js
The beginning of socket.io
2011
Npm
hits version 1.oBig companies started adopting Node: Linkedin,uber,etc
The journey continued ...
Node.js vs Browser
Both the browser and Node.js use JavaScript as their programming language. Building apps that run in the browser is a completely different thing than building a Node.js application. Despite the fact that it's always JavaScript, there are some key differences that make the experience radically different. From the perspective of a frontend developer who extensively uses JavaScript, Node.js apps bring with them a huge advantage: the comfort of programming everything - the frontend and the backend - in a single language. You have a huge opportunity because we know how hard it is to fully, deeply learn a programming language, and by using the same language to perform all your work on the web - both on the client and on the server, you're in a unique position of advantage. What changes is the ecosystem.
- In the browser, most of the time what you are doing is interacting with the DOM, or other Web Platform APIs like Cookies. Those do not exist in Node, of course. You don't have the document , window and all the other objects that are provided by the browser.
- And in the browser, we don't have all the nice APIs that Node.js provides through its modules, like the filesystem access functionality.
- Another big difference is that in Node.js you control the environment. Unless you are building an open source application that anyone can deploy anywhere, you know which version of Node.js you will run the application on. Compared to the browser environment, where you don't get the luxury to choose what browser your visitors will use, this is very convenient.
Node.js use cases
Data streaming
The problem
Streaming data in web require heavy processing. Data streaming is a complex process because it requires a continuous stream of data to be generated by an array of different sources and devices, delivered in a wide myriad of formats.
The solution
Node.js streams make data-streaming easier than ever. In traditional Media streaming, HTTP requests and responses are treated like isolated events; however, in reality, they are streams. Node.js can be used to build some cool data streaming features where files can still be processed while they’re being uploaded. This is possible because data comes in through a stream and can be processed online without being interrupted. For instance: real-time audio or video encoding is possible, particularly with the JavaScript library such as Node. Media and Entertainment is such an industry where Node.js can be utilized in Data streaming.
Server side proxy
The problem
Third-party proxies can cause troubles in building complex web-apps. Third-party proxy services such as Nginx and HAProxy are sometimes not feasible and scalable in handling multiple requests from various sources at a time.
The solution
Node.js makes it easier to build a proxy server Node.js can be easily employed as a server-side proxy for collecting data from various third-party resources. Whether you are building a news websites such as BBC, a media website such as Forbes, and entertainment websites such as IMDb, you would want to load content from various third-party domains. Node.js is beneficial for proxying different services with different response times. When used as a server-side proxy, it is capable of handling a large number of simultaneous connections in a non-blocking manner.
For instance: consider a website such as BBC news communicating with various third-party resources, gathering data from different sources, or storing assets like images and videos to third-party cloud services.
- Big data and analytics
- Real time data
- Queued I/O
- Chatbots
- Web scraping and automation
- REST API Node.js can be used in different systems like e-commerce, banking systems, chatting applications and many more.
Installing Node.js.
Node.js can be installed in different ways. This post highlights the most common and convenient ones. Official packages for all the major platforms are available at https://nodejs.dev/download/.
One very convenient way to install Node.js is through a package manager. In this case, every operating system has its own. Other package managers for MacOS, Linux, and Windows are listed in https://nodejs.dev/download/package-manager/
nvm
is a popular way to run Node.js. It allows you to easily switch the Node.js version, and install new versions to try and easily rollback if something breaks. It is also very useful to test your code with old Node.js versions.
See https://github.com/nvm-sh/nvm for more information about this option.
In any case, when Node.js is installed you'll have access to the node
executable program in the command line.
Running Node.js code
File name server.js
const http = require("http");
const host = 'localhost';
const port = 8000;
const requestListener = function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('Hello, World!');
}
const server = http.createServer(requestListener);
server.listen(port, host, () => {
console.log(`Server is running on http://${host}:${port}`);
});
Running the file: Open the terminal and run
node server.js
Output in the terminal
Server is running on http://localhost:8000
Output in the browser
Hello, world!
Summary
Node.js is built on Chrome’s V8 JavaScript engine, Node.js is an asynchronous event-driven JavaScript runtime. By using the event-callback/non-blocking approach, Node.js offers a single-threaded event-io model that allows orchestration of tasks running in parallel. It supports multiple connections without a need for a large memory footprint. We have seen many use cases of Node.js and how to write our first server using Node.js.
You can subscribe to our newsletter, like, comment and share if you find this blog helpful.