Node.js – Introduction

Node.js – Introduction ”; Previous Next What is Node.js? Node.js is a server-side runtime environment built on Google Chrome”s JavaScript Engine (V8 Engine). Node.js was developed by Ryan Dahl in 2009 and its latest version is v20.9.0. Node.js is a cross-platform (run on Windows, Linux, Unix, macOS, and more), open-source, back-end JavaScript runtime environment, that executes JavaScript code outside a web browser. The definition of Node.js as supplied by its official documentation is as follows − Node.js is a platform built on Chrome”s JavaScript runtime for easily building fast and scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices. Node.js environment is event-driven and provides non-blocking I/O, that optimizes throughput and scalability in web applications. The OpenJS Foundation, facilitated by the Linux Foundation”s Collaborative Projects program, now handles the Node.js distributed development. Features of Node.js Following are some of the important features that make Node.js the first choice of software architects. Asynchronous and Event Driven − All APIs of Node.js library are asynchronous, that is, non-blocking. It essentially means a Node.js based server never waits for an API to return data. The server moves to the next API after calling it and a notification mechanism of Events of Node.js helps the server to get a response from the previous API call. Very Fast − Being built on Google Chrome”s V8 JavaScript Engine, Node.js library is very fast in code execution. Single Threaded but Highly Scalable − Node.js uses a single threaded model with event looping. Event mechanism helps the server to respond in a non-blocking way and makes the server highly scalable as opposed to traditional servers which create limited threads to handle requests. Node.js uses a single threaded program and the same program can provide service to a much larger number of requests than traditional servers like Apache HTTP Server. No Buffering − Node.js applications never buffer any data. These applications simply output the data in chunks. License − Node.js is released under the MIT license. The following diagram depicts some important parts of Node.js which we will discuss in detail in the subsequent chapters. Where to Use Node.js? Following are the areas where Node.js is proving itself as a perfect technology partner. I/O bound Applications Data Streaming Applications Data Intensive Real-time Applications (DIRT) JSON APIs based Applications Single Page Applications However, it is not advisable to use Node.js for CPU intensive applications. Node.js is primarily used to build network programs such as Web servers. However, you can build different types of applications such as command line applications, web applications, real-time chat applications, REST APIs etc. Thousands of open-source libraries for Node.js are available, most of them hosted on the npm website, npm is a package manager for the JavaScript programming language. A number web frameworks can be used to accelerate the development of applications. Some of the popular frameworks are Express.js, Feathers.js, Koa.js, Sails.js, Meteor, and many others. Number of IDEs such as Atom, JetBrains WebStorm, NetBeans, and Visual Studio Code support development of Node.js applications. Cloud-hosting platforms like Google Cloud Platform and AWS Elastic Beanstalk can be used to host Node.js applications. Print Page Previous Next Advertisements ”;

Node.js – Command Line Options

Node.js – Command Line Options ”; Previous Next Any JavaScript file (with .js extension) can be executed from the command prompt, using it as a command line option to the node executable. PS D:nodejs> node hello.js You can invoke the Node.js REPL by running node without any option. PS D:nodejs> node > In addition, there are many options that can be used in the node command line. To get the available command line options, use –help PS D:nodejs> node –help Some of the frequently used command line options (sometimes also called switches) are as follows − Display version PS D:nodejs> node -v v20.9.0 PS D:nodejs> node –version v20.9.0 Evaluate script PS D:nodejs> node –eval “console.log(123)” 123 PS D:nodejs> node -e “console.log(123)” 123 Show help PS D:nodejs> node -h PS D:nodejs> node –help Start REPL PS D:nodejs> node -i PS D:nodejs> node –interactive Load module PS D:nodejs> node -r “http” PS D:nodejs> node –require “http” You can pass arguments to the script to be executed from the command line. The arguments are stored in a an array process.argv. The 0th element in the array is the nide executable, first element is the javascript file, followed by the arguments passed. Save the following script as hello.js and run it from command line, pass a string argument to it from command line. const args = process.argv; console.log(args); const name = args[2]; console.log(“Hello,”, name); In the terminal, enter PS D:nodejs> node hello.js TutorialsPoint [ ”C:\nodejs\node.exe”, ”D:\nodejs\a.js”, ”TutorialsPoint” ] Hello, TutorialsPoint You can also accept input from the command line in Node.js. Node.js since version 7 provides the readline module for this purpose. The createInterface() method helps in setting input from a readable stream such as the process.stdin stream, which during the execution of a Node.js program is the terminal input, one line at a time. Save the following code as hello.js const readline = require(”readline”).createInterface({ input: process.stdin, output: process.stdout, }); readline.question(`What”s your name?`, name => { console.log(`Hi ${name}!`); readline.close(); }); The question() method shows the first parameter (a question) and waits for the user input. It calls the callback function once enter is pressed. Run from command line. Node runtime waits for user input and then echoes the output on the console. PS D:nodejs> node a.js What”s your name?TutorialsPoint Hi TutorialsPoint! You can also set environment variables from the command line. Assign the values to one or more variables before the node executable name. USER_ID=101 USER_NAME=admin node app.js Inside the script, the environment variables are available as the properties of the process.env object. process.env.USER_ID; // “101” process.env.USER_NAME; // “admin” Print Page Previous Next Advertisements ”;