Node.js – Streams ”; Previous Next What are Streams? A stream is a collection of data. However, unlike an array or a string, the entire data in a stream object is not stored at once in the memory. Instead, a single chunk of data from the stream is brought into the memory, at a time. It makes streams more efficient. Node.js applications are best suited for developing data streaming applications. Types of streams Node.js processes four fundamental types of streams − Writable − streams to which data can be written. Readable − streams from which data can be read. Duplex − streams that are both Readable and Writable. Transform − Duplex streams that can modify or transform the data as it is written and read. Each type of Stream is an EventEmitter instance and throws several events at different instance of times. For example, some of the commonly used events are − data − This event is fired when there is data is available to read. end − This event is fired when there is no more data to read. error − This event is fired when there is any error receiving or writing data. finish − This event is fired when all the data has been flushed to underlying system. The examples in this chapter use a file named input.text with the following data in it. Tutorials Point is giving self learning content to teach the world in simple and easy way!!!!! Readable Stream The file object acts as a stream from which the data can be read in a chunk of specific size. In the following example, we call createReadStream() function from fs module to read data from a given file. The on event of the readable stream collects the file contents till the end event is fired. var fs = require(“fs”); var data = ””; // Create a readable stream var readerStream = fs.createReadStream(”input.txt”); // Set the encoding to be utf8. readerStream.setEncoding(”UTF8”); // Handle stream events –> data, end, and error readerStream.on(”data”, function(chunk) { data += chunk; }); readerStream.on(”end”,function() { console.log(data); }); readerStream.on(”error”, function(err) { console.log(err.stack); }); console.log(“Program Ended”); Save the above script as main.js. Run the above Node.js application. It will display the contents of input.text file. Writable Stream The createWriteStream() function from fs module creates an object of writable stream. Its write() method stores the data in the file given to the createWriteStream() function as an argument. Save the following code with main.js as its name. var fs = require(“fs”); var data = `Tutorials Point is giving self learning content to teach the world in simple and easy way!!!!!`; // Create a writable stream var writerStream = fs.createWriteStream(”output.txt”); // Write the data to stream with encoding to be utf8 writerStream.write(data,”UTF8”); // Mark the end of file writerStream.end(); // Handle stream events –> finish, and error writerStream.on(”finish”, function() { console.log(“Write completed.”); }); writerStream.on(”error”, function(err){ console.log(err.stack); }); console.log(“Program Ended”); Now open output.txt created in your current directory to check if it contains the given data. Piping the Streams Piping is a mechanism where we provide the output of one stream as the input to another stream. It is normally used to get data from one stream and to pass the output of that stream to another stream. There is no limit on piping operations. Now we”ll show a piping example for reading from one file and writing it to another file. Create a js file named main.js with the following code − var fs = require(“fs”); // Create a readable stream var readerStream = fs.createReadStream(”input.txt”); // Create a writable stream var writerStream = fs.createWriteStream(”output.txt”); // Pipe the read and write operations // read input.txt and write data to output.txt readerStream.pipe(writerStream); console.log(“Program Ended”); Now run the main.js to see the result − node main.js Verify the Output. Program Ended Open output.txt created in your current directory; it should contain the following − Tutorials Point is giving self learning content to teach the world in simple and easy way!!!!! Chaining the Streams Chaining is a mechanism to connect the output of one stream to another stream and create a chain of multiple stream operations. It is normally used with piping operations. Now we”ll use piping and chaining to first compress a file and then decompress the same. Create a js file named main.js with the following code − var fs = require(“fs”); var zlib = require(”zlib”); // Compress the file input.txt to input.txt.gz fs.createReadStream(”input.txt”) .pipe(zlib.createGzip()) .pipe(fs.createWriteStream(”input.txt.gz”)); console.log(“File Compressed.”); Now run the main.js to see the result − node main.js Verify the Output. File Compressed. You will find that input.txt has been compressed and it created a file input.txt.gz in the current directory. Now let”s try to decompress the same file using the following code − var fs = require(“fs”); var zlib = require(”zlib”); // Decompress the file input.txt.gz to input.txt fs.createReadStream(”input.txt.gz”) .pipe(zlib.createGunzip()) .pipe(fs.createWriteStream(”input.txt”)); console.log(“File Decompressed.”); Now run the main.js to see the result − node main.js Verify the Output. File Decompressed. Print Page Previous Next Advertisements ”;
Category: Computer Programming
Node.js – Express Framework
Node.js – Express Framework ”; Previous Next Express.js is a minimal and flexible web application framework that provides a robust set of features to develop Node.js based web and mobile applications. Express.js is one of the most popular web frameworks in the Node.js ecosystem. Express.js provides all the features of a modern web framework, such as templating, static file handling, connectivity with SQL and NoSQL databases. Node.js has a built-in web server. The createServer() method in its http module launches an asynchronous http server. It is possible to develop a web application with core Node.js features. However, all the low level manipulations of HTTP request and responses have to be tediously handled. The web application frameworks take care of these common tasks, allowing the developer to concentrate on the business logic of the application. A web framework such as Express.js is a set of utilities that facilitates rapid, robust and scalable web applications. Following are some of the core features of Express framework − Allows to set up middlewares to respond to HTTP Requests. Defines a routing table which is used to perform different actions based on HTTP Method and URL. Allows to dynamically render HTML Pages based on passing arguments to templates. The Express.js is built on top of the connect middleware, which in turn is based on http, one of the core modules of Node.js API. Installing Express The Express.js package is available on npm package repository. Let us install express package locally in an application folder named ExpressApp. D:expressApp> npm init D:expressApp> npm install express –save The above command saves the installation locally in the node_modules directory and creates a directory express inside node_modules. Hello world Example Following is a very basic Express app which starts a server and listens on port 5000 for connection. This app responds with Hello World! for requests to the homepage. For every other path, it will respond with a 404 Not Found. var express = require(”express”); var app = express(); app.get(”/”, function (req, res) { res.send(”Hello World”); }) var server = app.listen(5000, function () { console.log(“Express App running at http://127.0.0.1:5000/”); }) Save the above code as index.js and run it from the command-line. D:expressApp> node index.js Express App running at http://127.0.0.1:5000/ Visit http://localhost:5000/ in a browser window. It displays the Hello World message. Application object An object of the top level express class denotes the application object. It is instantiated by the following statement − var express = require(”express”); var app = express(); The Application object handles important tasks such as handling HTTP requests, rendering HTML views, and configuring middleware etc. The app.listen() method creates the Node.js web server at the specified host and port. It encapsulates the createServer() method in http module of Node.js API. app.listen(port, callback); Basic Routing The app object handles HTTP requests GET, POST, PUT and DELETE with app.get(), app.post(), app.put() and app.delete() method respectively. The HTTP request and HTTP response objects are provided as arguments to these methods by the NodeJS server. The first parameter to these methods is a string that represents the endpoint of the URL. These methods are asynchronous, and invoke a callback by passing the request and response objects. GET method In the above example, we have provided a method that handles the GET request when the client visits ”/” endpoint. app.get(”/”, function (req, res) { res.send(”Hello World”); }) Request Object − The request object represents the HTTP request and has properties for the request query string, parameters, body, HTTP headers, and so on. Response Object − The response object represents the HTTP response that an Express app sends when it gets an HTTP request. The send() method of the response object formulates the server”s response to the client. You can print request and response objects which provide a lot of information related to HTTP request and response including cookies, sessions, URL, etc. The response object also has a sendFile() method that sends the contents of a given file as the response. res.sendFile(path) Save the following HTML script as index.html in the root folder of the express app. <html> <body> <h2 style=”text-align: center;”>Hello World</h2> </body> </html> Change the index.js file to the code below − var express = require(”express”); var app = express(); var path = require(”path”); app.get(”/”, function (req, res) { res.sendFile(path.join(__dirname,”index.html”)); }) var server = app.listen(5000, function () { console.log(“Express App running at http://127.0.0.1:5000/”); }) Run the above program and visit http://localhost:5000/, the browser shows Hello World message as below: Let us use sendFile() method to display a HTML form in the index.html file. <html> <body> <form action = “/process_get” method = “GET”> First Name: <input type = “text” name = “first_name”> <br> Last Name: <input type = “text” name = “last_name”> <br> <input type = “submit” value = “Submit”> </form> </body> </html> The above form submits the data to /process_get endpoint, with GET method. Hence we need to provide a app.get() method that gets called when the form is submitted. app.get(”/process_get”, function (req, res) { // Prepare output in JSON format response = { first_name:req.query.first_name, last_name:req.query.last_name }; console.log(response); res.end(JSON.stringify(response)); }) The form data is included in the request object. This method retrieves the data from request.query array, and sends it as a response to the client. The complete code for index.js is as follows − var express = require(”express”); var app = express(); var path = require(”path”); app.use(express.static(”public”)); app.get(”/”, function (req, res) { res.sendFile(path.join(__dirname,”index.html”)); }) app.get(”/process_get”, function (req, res) { // Prepare output in JSON format response = { first_name:req.query.first_name, last_name:req.query.last_name }; console.log(response); res.end(JSON.stringify(response)); }) var server = app.listen(5000, function () { console.log(“Express App running at http://127.0.0.1:5000/”); }) Visit http://localhost:5000/. Now you can enter the First and Last Name and then click submit button to see the result and it should return the following result − {“first_name”:”John”,”last_name”:”Paul”} POST method The HTML form is normally used to submit the data to the server, with its method parameter set to POST, especially when some binary data such as images is to be submitted. So, let us change
Node.js – Process
Node.js – Process ”; Previous Next The process object in Node.js is a global object, although it is defined in process module. It is an instance of EventEmitter class. The process object provides information on current Node.js process. With the help of a number of methods and properties associated with this object, it is possible to control the current Node.js process. Process Events The process object is an instance of EventEmitter and emits the following events − Sr.No. Event & Description 1 exit Emitted when the process is about to exit. There is no way to prevent the exiting of the event loop at this point, and once all exit listeners have finished running, the process will exit. 2 beforeExit This event is emitted when node empties its event loop and has nothing else to schedule. Normally, the node exits when there is no work scheduled, but a listener for ”beforeExit” can make asynchronous calls, and cause the node to continue. 3 uncaughtException Emitted when an exception bubbles all the way back to the event loop. If a listener is added for this exception, the default action (which is to print a stack trace and exit) will not occur. 4 warning The ”warning” event is emitted whenever Node.js emits a process warning. A process warning is similar to an error in that it describes exceptional conditions that are being brought to the user”s attention. 5 Signal Events Emitted when the processes receives a signal such as SIGINT, SIGHUP, etc. Example In the following code, the beforeExit event of the process object is associated with a callback arrow function. Similarly the exit event calls another callback function. The function on beforeExit runs first, followed by the one on exit event. process.on(”beforeExit”, (code) => { console.log(”A beforeExit event occured with code: ”, code); }); process.on(”exit”, (code) => { console.log(”Process exit event with code: ”, code); }); console.log(”This message is displayed first.”); Output This message is displayed first. A beforeExit event occured with code: 0 Process exit event with code: 0 Note that the listener functions must only perform synchronous operations. The Node.js process will exit immediately after calling the ”exit” event listeners causing any additional work still queued in the event loop to be abandoned. In the following example, for instance, the timeout will never occur − Example process.on(”exit”, function(code) { // Following code will never execute. setTimeout(function() { console.log(“This will not run”); }, 0); console.log(”About to exit with code:”, code); }); console.log(“Program Ended”); Output Program Ended About to exit with code: 0 Process Methods abort() The abort() method immediately terminates the current process and then creates a core file. Example The following program prints a message to the console after every one second, and terminates the loop after 5 seconds as the abort() function is called. const abortfunction = () => { console.log(”Start…”); // It prints the message after every 1 second setInterval((function() { return console.log(”Hello World”); }), 1000); // It calls process.abort() after 5 seconds setTimeout((function() { return process.abort(); }), 5000); }; abortfunction(); Output Start… Hello World Hello World Hello World Hello World The termination is followed by a long core file output. chdir() This method changes the current working directory of the Node.js process or throws an exception if doing so fails (for instance, if the specified directory does not exist). cwd() The process.cwd() method returns the current working directory of the Node.js process. Example console.log(`Starting directory: ${process.cwd()}`); try { process.chdir(”NewNodeApp”); console.log(`New directory: ${process.cwd()}`); } catch (err) { console.error(`chdir: ${err}`); } Output Starting directory: D:nodejs New directory: D:nodejsNewNodeApp If the directory is not found, the output will be as follows − Starting directory: D:nodejs chdir: Error: ENOENT: no such file or directory, chdir ”D:nodejs” -> ”NewDir” exit() This method terminates the current process synchronously with an exit status of code (default is ”success” code 0). Node.js will not terminate until all the ”exit” event listeners are called. Example console.log(”Code running”); process.on(”exit”, function(code) { return console.log(`exiting with the error code : ${code}`); }); setTimeout((function() { return process.exit(0); //after 5 sec }), 5000); Output Code running exiting with the error code : 0 kill() This method terminates the current process and sends a signal to the process identified by pid. kill(pid[, signal]) Parameters pid : A process ID signal : The signal to send, either as a string or number. Default: ”SIGTERM”. Signal names are strings such as ”SIGINT” or ”SIGHUP”. Example The following code obtains the pid of the current process. It waits for a long enough duration before it exits normally. In between if you emit SIGINT signal by pressing ctrl-C, the process doesn’t terminate. const pid = process.pid; console.log(`Process ID: ${pid}`); process.on(”SIGHUP”, () => console.log(”Received: SIGHUP”)); process.on(”SIGINT”, () => console.log(”Received: SIGINT”)); setTimeout(() => {}, 100000); // keep process alive setTimeout((function() { return process.kill(pid, ”SIGINT”); //after 5 sec }), 5000); The terminal shows the process ID and Received: SIGINT whenever ctrlC is pressed. After another timeout of 5 seconds, the kill() method is called, which terminates the process Process ID: 1776 Received: SIGINT memoryUsage() This function returns an object describing the memory usage of the Node.js process measured in bytes. Example console.log(process.memoryUsage()); Output { rss: 24768512, heapTotal: 4079616, heapUsed: 3153848, external: 1097184, arrayBuffers: 10519 } nextTick() This function defers the execution of a callback function until the next Event Loop Iteration. nextTick(callback[, …args]) This function is an important part of the event loop in asynchronous API of Node.js. In Node.js, each iteration of an Event Loop is called a tick. To schedule a callback function to be invoked in the next iteration of the Event Loop, we use process.nextTick(). Example console.log(”start”); process.nextTick(() => { console.log(”nextTick callback executed in next iteration”); }); console.log(”scheduled”); Output start scheduled nextTick callback executed in next iteration Process properties process.arch The operating system CPU architecture for which the Node.js binary was compiled. Possible values are − ”arm”, ”arm64”, ”ia32”, ”loong64”, ”mips”, ”mipsel”, ”ppc”, ”ppc64”, ”riscv64”, ”s390”, ”s390x”, ”x64” Example console.log(`This processor architecture is ${process.arch}`); Output This processor architecture is x64 process.argv
Node.js – Event Loop
Node.js – Event Loop ”; Previous Next Even though JavaScript is single threaded, Node.js employs event loop to perform asynchronous non-blocking I/O operations, by delegating the operations to the system kernel whenever possible. Most modern OS kernels are multi-threaded, capable of handling multiple operations by executing them in the background. When the current operation is completed, the kernel informs Node.js so that the appropriate callback may be added to the poll queue to eventually be executed. The event loop is initialized as soon as Node.js starts, either by providing a .js script or in REPL mode. The order of operations of the event loop are shown in the figure below − The Timers phase executes callbacks scheduled by setTimeout() and setInterval(). The pending callbacks phase executes I/O callbacks deferred to the next loop iteration. The poll phase has two main functions: (a) calculating how long it should block and poll for I/O, and (b) processing events in the poll queue. Node.js retrieves new I/O events and executes I/O related callbacks in this phase. The check phase executes the callbacks immediately after the poll phase has completed. If the poll phase becomes idle and scripts have been queued with setImmediate() timer. The event loop continues to the check phase rather than waiting. The libuv library is a part of Node.js runtime, playing the role of providing support for handling asynchronous operations. The V8 engine handles the execution of JavaScript code, whereas the Libuv library utilizes the native mechanism of the respective operating system to hanle asynchronous operations. Finally, the close callbacks phase handles the callbacks registered with close event such as socket.on(‘close’, function). The close event will be emitted if the socket is closed abruptly, otherwise it will be emitted by process.nextTick() method to defer the execution of a function until the next iteration of the event loop. Before beginning the next run of the event loop, Node.js checks if it is waiting for any asynchronous I/O or timers. If there aren’t any, the runtime shuts down cleanly. Understanding how the event loop works is essential for building scalable Node.js applications. The event loop is a fundamental part of Node.js that enables asynchronous programming by ensuring the main thread is not blocked. Print Page Previous Next Advertisements ”;
Node.js – MySQL Get Started
Node.js – MySQL Get Started ”; Previous Next Any server-side programming language like Node.js needs to interface with a backend database for persistent data storage and retrieval, based on its interaction with the client-side application. MySQL is widely used RDBMS software. In this chapter, we shall learn how to connect a Node.js application with a MySQL database. MySQL is an open-source and cross-platform relational database management system, and uses SQL (Structured Query Language) to create, modify and extract data from the tables in e relational database, as well as control user access to the database. MySQL also works with an operating system to implement the management features of a relational database such as allocation of storage, manage users, grant network access and creation of backups. MySQL Installation To use MySQL as a backend with a Node.js application, you must install MySQL on your computer. Let us learn how to install MySQL on Linux and Windows. On Ubuntu Linux Update package information from the MySQL APT repository with this command − $ sudo apt-get update Install MySQL by the following command − $ sudo apt-get install mysql-server This installs the package for the MySQL server, as well as the packages for the client and for the database common files. During the installation, you are asked to supply a password for the root user for your MySQL installation. The MySQL server is started automatically after installation. You can check the status of the MySQL server with the following command − $ systemctl status mysql Then install the MySQL Workbench − $ sudo apt-get install mysql-workbench-community On Windows Download the latest community edition of MySQL installer for Windows from the link https://dev.mysql.com/downloads/mysql/ The MSI installer (mysql-installer-community-8.0.35.0.msi) will be downloaded. Start the installation wizard by double-clicking it. Choose “Full” setup type from the screen below − Installer shows a list of products to be installed: During the installation, choose an appropriate root password that is both strong and one you can remember. Here is the final step in the installation wizard − The MySQL Workbench will now start. It is an integrated environment for MySQL. It enables users to graphically administer MySQL databases and visually design database structures. Connect to the MySQL server running on your machine by entering the password − The Schema tab shows the databases that are currently available on the server. The MySQL command line client is also installed during the installation. When you start it, you”ll be required to input the root password. The mysql prompt appears. Now you can enter any MySQL command in front of it. To list all the databases, use the command show databases; mysql> show databases; +——————–+ | Database | +——————–+ | information_schema | | mysql | | performance_schema | | sakila | | sys | | world | +——————–+ 6 rows in set (0.00 sec) mysql> You can create a new database with create database command − mysql> create database mydb; Query OK, 1 row affected (0.01 sec) MySQL Driver for Node.js To use MySQL database as a backend, you need to install and include a MySQL driver from Node Package Manager (NPM). The NPM repository hosts mysql module, it is a node.js driver for mysql. It is written in JavaScript, does not require compiling, and is 100% MIT licensed. If you have installed the latest version of Node.js runtime (V10 or newer), npm is already available. To install the mysql module, use the following command in the command terminal − npm install mysql Note − that the official website of MySQL also provides a Connector/Node.js X DevAPI driver. From the version 8.0 onwards, MySQL supports NoSQL Document Store. The Connector/Node.js driver is especially needed if you intend to use the NoSQL features of MySQL. In this tutorial, we are using the mysql module, installed from NPM. Establishing Connection First step is to import the mysql module in your code by the following require() statement − var mysql = require(”mysql”); The createConnection() function in this module uses the credentials provided as arguments and returns the connection object. createConnection(connectionUri: string | mysql.ConnectionConfig): mysql.Connection The most common usage of createConnection() function is as follows − var con = mysql.createConnection({ host: “localhost”, user: “root”, password: “mypassword” }); You have to use the connect() method of the connection object. con.connect(function (err) { if (err) throw err; console.log(“Connected!”); }); If the connection is successful, you’ll get “Connected!” message in the console. Print Page Previous Next Advertisements ”;
Node.js – Global Objects
Node.js – Global Objects ”; Previous Next Global objects in Node.js are built-in objects. The Node.js runtime is made up of a number of core modules. To incorporate the functionality of any of the core modules such as fs module, or http module (or any external module installed from npm – such as express module), you need to load the same with require() function. However, some modules, functions, classes, variables etc. can be used directly in the JavaScript code, without loading them with require() functions. They are called Global objects. Let us take a look at the global classes, variables and functions in Node.js Buffer class Buffer class is a global class that can be accessed in an application without importing the buffer module. An object of buffer class stores raw data similar to an array of integers but corresponds to a raw memory allocation outside the V8 heap. You can create an uninitiated Buffer of 10 octets − var buf = new Buffer(10); A buffer object can also be created from a given array − var buf = new Buffer([10, 20, 30, 40, 50]); or from a given string − var buf = new Buffer(“Simply Easy Learning”, “utf-8″); You can perform different operations with a buffer such as read/write data, convert a buffer to JSON, concatenation of buffers etc. Console class A Console class with methods such as console.log(), console.error(), and console.warn() that can be used to write to any Node.js stream. The global console can be used without calling require(”node:console”). The following REPL session shows the function of the global console object > console.log(“Hello World”); Hello World undefined > console.error(“Some error occurred”); Some error occurred undefined > console.warn(“This is a warning message!”); This is a warning message! undefined Process object The process object is a global object, although it is defined in process module. It is an instance of EventEmitter class. The process object provides information on current process. With the help of a number of methods and properties associated with this object, it is possible to control the current process. One of the properties of Process object is argv array. It stores the command-line arguments passed to the node executable. The 0th element in the array is the node 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 The process.argv0 property stores a read-only copy of the original value of argv[0] passed when Node.js starts. The env property of Process object stores the environment variables. You can 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” Some useful information about the current process is stored in the properties of process object, as shown in the following example. Example console.log(”Process Architecture:”+process.arch); console.log(”Current working directory:”+ process.cwd()); console.log(”Process PID: ”+process.pid); console.log(”Process Platform: ”+process.platform); console.log(”Process Version: ”+process.version); Output Process Architecture:x64 Current working directory:D:nodejs Process PID: 11060 Process Platform: win32 Process Version: v20.9.0 Global timer functions The timer module of Node.js defines functions for scheduling callbacks. They can be used as global functions (without importing the module). The setTimeout() function is used to run a callback after a specified duration in milliseconds. function printHello() { console.log( “Hello, World!”); } // Now call above function after 2 seconds setTimeout(printHello, 2000); The clearTimeout() function is used to stop a timer that was previously created with setTimeout(). The setInterval() function is used to run a callback repeatedly after a specified duration in milliseconds. function printHello() { console.log( “Hello, World!”); } // Now call above function after 2 seconds setInterval(printHello, 2000); Global variables __filename The __filename represents the filename of the code being executed. This is the resolved absolute path of this code file. For a main program, this is not necessarily the same filename used in the command line. The value inside a module is the path to that module file. Example Create a js file named main.js with the following code − // Let”s try to print the value of __filename console.log( __filename ); Now run the main.js to see the result − D:nodejsmain.js __dirname The __dirname represents the name of the directory that the currently executing script resides in. Example Create a js file named main.js with the following code − // Let”s try to print the value of __dirname console.log( __dirname ); Now run the main.js to see the result − D:nodejs Note that require() function, which imports a given module into the Node.js runtime, is also a global function. Global Objects The following table provides a list of other objects which we use frequently in our applications. For a more detail, you can refer to the official documentation. Sr.No. Module Name & Description 1 Console Used to print information on stdout and stderr. 2 Process Used to get information on current process. Provides multiple events related to process activities. Print Page Previous Next Advertisements ”;
Node.js – Scaling Application ”; Previous Next Scalability of a Node.js application refers to its ability to handle increasing workload on the server with a greater number of requests received from clients. A Node.js application can utilize child processes to address this issue. A child process is a separate instance of the Node.js runtime that can be spawned and managed by the main process. Such a spawned child process can perform specific tasks parallel, thereby it improves the overall performance and scalability of the application. The child_process module is one of the core modules of Node.js runtime. This module provides various methods for creating, managing, and communicating with child processes, especially on multi-core CPU based systems. A child process always has three streams: child.stdin, child.stdout, and child.stderr. These streams may be shared with the streams of the parent process. The child_process module which has the following three major ways to create a child process. exec − child_process.exec method runs a command in a shell/console and buffers the output. spawn − child_process.spawn launches a new process with a given command. fork − The child_process.fork method is a special case of the spawn() to create child processes. The exec() method child_process.exec method runs a command in a shell and buffers the output. It has the following signature − child_process.exec(command[, options], callback) Parameters command − The command to run, with space-separated arguments. options − cwd- the current working directory of the child process, and env – Environment key-value pairs encoding − encoding string (Default: ”utf8”) shell − Shell to execute the command with (Default: ”/bin/sh” on UNIX, ”cmd.exe” on Windows, The shell should understand the -c switch on UNIX or /s /c on Windows. On Windows, command line parsing should be compatible with cmd.exe.) timeout − Default: 0 maxBuffer − Default: 200*1024 killSignal − Default: ”SIGTERM” uid − Sets the user identity of the process. gid − Sets the group identity of the process. callback − A function with three arguments error, stdout, and stderr which are called with the output when the process terminates. The exec() method returns a buffer with a max size and waits for the process to end and tries to return all the buffered data at once. Example Let us create two js files named child.js and main.js − File: child.js console.log(“Starting Child Process-” + process.argv[2]); console.log(“Child Process-” + process.argv[2] + ” executed.” ); File: main.js const fs = require(”fs”); const child_process = require(”child_process”); for(var i=0; i<3; i++) { var childprocess = child_process.exec(”node child.js ”+i,function (error, stdout, stderr) { if (error) { console.log(error.stack); console.log(”Error code: ”+error.code); console.log(”Signal received: ”+error.signal); } console.log(”stdout: ” + stdout); console.log(”stderr: ” + stderr); }); childprocess.on(”exit”, function (code) { console.log(”Child process exited with exit code ”+code); }); } Now run the main.js to see the result − Child process exited with exit code 0 stdout: Starting Child Process-0 Child Process-0 executed. stderr: Child process exited with exit code 0 stdout: Starting Child Process-1 Child Process-1 executed. stderr: Child process exited with exit code 0 stdout: Starting Child Process-2 Child Process-2 executed. stderr: The spawn() Method child_process.spawn method launches a new process with a given command. child_process.spawn(command[, args][, options]) The first argument is the command to be run by the child process. One or more arguments may be passed to it. The options arguments may have one or more of the following properties − cwd (String) Current working directory of the child process. env (Object) Environment key-value pairs. stdio (Array) String Child”s stdio configuration. customFds (Array) Deprecated File descriptors for the child to use for stdio. detached (Boolean) The child will be a process group leader. uid (Number) Sets the user identity of the process. gid (Number) Sets the group identity of the process. The spawn() method returns streams (stdout &stderr) and it should be used when the process returns a volume amount of data. spawn() starts receiving the response as soon as the process starts executing. Example As in the previous example, create child.js and main.js files. child.js console.log(“Starting Child Process-” + process.argv[2]); console.log(“Child Process-” + process.argv[2] + ” executed.” ); main.js const fs = require(”fs”); const child_process = require(”child_process”); for(var i=0; i<3; i++) { var childprocess = child_process.spawn(”node”, [”child.js”, i]); childprocess.stdout.on(”data”, function (data) { console.log(”stdout: ” + data); }); childprocess.on(”exit”, function (code) { console.log(”Child process exited with exit code ”+code); }); } Output stdout: Starting Child Process-0 stdout: Child Process-0 executed. Child process exited with exit code 0 stdout: Starting Child Process-1 stdout: Child Process-1 executed. stdout: Starting Child Process-2 stdout: Child Process-2 executed. Child process exited with exit code 0 Child process exited with exit code 0 The fork() Method child_process.fork method is a special case of spawn() to create Node processes. It has the following syntax − child_process.fork(modulePath[, args][, options]) Parameters The first parameter is the module to be tun by the child. You may pass additional arguments to the module. The options are the same as in the spawn() method. Example As in the previous example, create child.js and main.js files. child.js console.log(“Starting Child Process-” + process.argv[2]); console.log(“Child Process-” + process.argv[2] + ” executed.” ); main.js const fs = require(”fs”); const child_process = require(”child_process”); for(var i=0; i<3; i++) { var childprocess = child_process.fork(“child.js”, [i]); childprocess.on(”close”, function (code) { console.log(”child process exited with code ” + code); }); } Output Starting Child Process-0 Child Process-0 executed. Starting Child Process-2 Child Process-2 executed. Starting Child Process-1 Child Process-1 executed. child process exited with code 0 child process exited with code 0 child process exited with code 0 execFile() method The child_process.execFile() function is similar to the exec() method, except that it does not spawn a shell by default. It is slightly more efficient than the exec() as the specified executable file is spawned directly as a new process. Syntax child_process.execFile(command [, args][, options][, callback]) Parameters command − Accepts a string that specifies the name or path of the command to run. args − List of string arguments. options − Some of the options available are cwd, env, encoding,
Node.js – MySQL Create Database ”; Previous Next One or more databases (or schemas) can be created on a MySQL server. While we can create databases with MySQL query CREATE DATABASE, as well as with Workbench, we are interested in creating a database with a Node.js program. Databases are where we store our data in the form of tables. We use the MySQL query CREATE DATABASE to create a database. Syntax Following is the syntax of the MySQL Creating Database query − CREATE DATABASE IF NOT EXISTS database_name; Example Create a database of your choice, in here we are creating one for a pet store and hence calling it mydb and, running the command “CREATE DATABASE” on MySQL Command Line Client. mysql> CREATE DATABASE IF NOT EXISTS mydb; Query OK, 1 row affected (0.02 sec) To verify database creation, we can run the “SHOW DATABASES” query − mysql> show databases; +——————–+ | Database | +——————–+ | information_schema | | mydb | | mysql | | performance_schema | | sakila | | sys | | world | +——————–+ 7 rows in set (0.00 sec) con.query() method The connection object returned by mysql.createConnection() method has access to a query() method. A string representation of any valid MySQL query is passed to it as an argument. The second argument to query() method is a callback function. The query() method is used as follows − con.query(sql, function (err, result) {. . . . . . }); We just need to change the “sql” argument in the query() function with the desired query in the string format. Since the query to create a database in MySQL is “CREATE DATABASE $database_name$”, then, the syntax to create a database using the Node.js con.query() function becomes − con.query(“CREATE DATABASE $database_name$;”, function (err, result) { //Your code }); Example Following the Java Script example is trying to create a MySQL database named mydb. To do so − First of all, you need to replace the “sql” argument in the query function with “CREATE DATABASE mydb;” and run the file using node. Now, to check if you”ve created the database, we can use the MySQL query “SHOW DATABASES;”. For this, you need to execute the con.query() function again this time you need to pass the “SHOW DATABASES;” query as the first parameter. The SHOW DATABASES query returns an array of RowDataPacket objects. You need to stringify each object in the array to get the database name. Run a loop to look at all the databases with their names and find the database you”ve just created. We have added the IF NOT EXISTS clause while creating the database as we don”t want any error to occur during the creation of the database in case it has already been created. var mysql = require(”mysql”); var con = mysql.createConnection({ host: “localhost”, user: “root”, password: “mypassword” }); con.connect(function (err) { if (err) throw err; console.log(“Connected!”); con.query(“CREATE DATABASE IF NOT EXISTS mydb;”, function (err, result) { if (err) throw err; console.log(“Created database successfully”); }); con.query(“SHOW DATABASES;”, function (err, result) { if (err) throw err; console.log(“Showing databasesn”); for (var i = 0; i < result.length; i++) { console.log(JSON.stringify(result[i])); } }) con.end(); }); Output Connected! Created database successfully Showing databases {“Database”:”information_schema”} {“Database”:”mydb”} {“Database”:”mysql”} {“Database”:”performance_schema”} {“Database”:”sakila”} {“Database”:”sys”} {“Database”:”world”} Print Page Previous Next Advertisements ”;
Node.js – RESTFul API
Node.js – RESTful API ”; Previous Next A Node.js application using ExpressJS is ideally suited for building REST APIs. In this chapter, we shall explain what is a REST (also called RESTFul) API, and build a Node.js based, Express.js REST application. We shall also use REST clients to test out REST API. API is an acronym for Application Programming Interface. The word interface generally refers to a common meeting ground between two isolated and independent environments. A programming interface is an interface between two software applications. The term REST API or RESTFul API is used for a web Application that exposes its resources to other web/mobile applications through the Internet, by defining one or more endpoints which the client apps can visit to perform read/write operations on the host”s resources. REST architecture has become the de facto standard for building APIs, preferred by the developers over other technologies such as RPC, (stands for Remote Procedure Call), and SOAP (stands for Simple Object Access Protocol). What is REST architecture? REST stands for REpresentational State Transfer. REST is a well known software architectural style. It defines how the architecture of a web application should behave. It is a resource based architecture where everything that the REST server hosts, (a file, an image, or a row in a table of a database), is a resource, having many representations. REST was first introduced by Roy Fielding in 2000. REST recommends certain architectural constraints. Uniform interface Statelessness Client-server Cacheability Layered system Code on demand These are the advantages of REST constraints − Scalability Simplicity Modifiability Reliability Portability Visibility A REST Server provides access to resources and REST client accesses and modifies the resources using HTTP protocol. Here each resource is identified by URIs/ global IDs. REST uses various representation to represent a resource like text, JSON, XML but JSON is the most popular one. HTTP methods Following four HTTP methods are commonly used in REST based architecture. POST Method The POST verb in the HTTP request indicates that a new resource is to be created on the server. It corresponds to the CREATE operation in the CRUD (CREATE, RETRIEVE, UPDATE and DELETE) term. To create a new resource, you need certain data, it is included in the request as a data header. Examples of POST request − HTTP POST http://example.com/users HTTP POST http://example.com/users/123 GET Method The purpose of the GET operation is to retrieve an existing resource on the server and return its XML/JSON representation as the response. It corresponds to the READ part in the CRUD term. Examples of a GET request − HTTP GET http://example.com/users HTTP GET http://example.com/users/123 PUT Method The client uses HTTP PUT method to update an existing resource, corresponding to the UPDATE part in CRUD). The data required for update is included in the request body. Examples of a PUT request − HTTP PUT http://example.com/users/123 HTTP PUT http://example.com/users/123/name/Ravi DELETE Method The DELETE method (as the name suggest) is used to delete one or more resources on the server. On successful execution, an HTTP response code 200 (OK) is sent. Examples of a DELETE request − HTTP DELETE http://example.com/users/123 HTTP DELETE http://example.com/users/123/name/Ravi RESTful Web Services Web services based on REST Architecture are known as RESTful web services. These webservices uses HTTP methods to implement the concept of REST architecture. A RESTful web service usually defines a URI, Uniform Resource Identifier a service, which provides resource representation such as JSON and set of HTTP Methods. Creating RESTful API for A Library Consider we have a JSON based database of users having the following users in a file users.json: { “user1” : { “name” : “mahesh”, “password” : “password1”, “profession” : “teacher”, “id”: 1 }, “user2” : { “name” : “suresh”, “password” : “password2”, “profession” : “librarian”, “id”: 2 }, “user3” : { “name” : “ramesh”, “password” : “password3”, “profession” : “clerk”, “id”: 3 } } Our API will expose the following endpoints for the clients to perform CRUD operations on the users.json file, which the collection of resources on the server. Sr.No. URI HTTP Method POST body Result 1 / GET empty Show list of all the users. 2 / POST JSON String Add details of new user. 3 /:id DELETE JSON String Delete an existing user. 4 /:id GET empty Show details of a user. 5 /:id PUT JSON String Update an existing user List Users Let”s implement the first route in our RESTful API to list all Users using the following code in a index.js file var express = require(”express”); var app = express(); var fs = require(“fs”); app.get(”/”, function (req, res) { fs.readFile( __dirname + “/” + “users.json”, ”utf8”, function (err, data) { res.end( data ); }); }) var server = app.listen(5000, function () { console.log(“Express App running at http://127.0.0.1:5000/”); }) To test this endpoint, you can use a REST client such as Postman or Insomnia. In this chapter, we shall use Insomnia client. Run index.js from command prompt, and launch the Insomnia client. Choose GET methos and enter http://localhost:5000/ URL. The list of all users from users.json will be displayed in the Respone Panel on right. You can also use CuRL command line tool for sending HTTP requests. Open another terminal and issue a GET request for the above URL. C:Usersmlath>curl http://localhost:5000/ { “user1” : { “name” : “mahesh”, “password” : “password1”, “profession” : “teacher”, “id”: 1 }, “user2” : { “name” : “suresh”, “password” : “password2”, “profession” : “librarian”, “id”: 2 }, “user3” : { “name” : “ramesh”, “password” : “password3”, “profession” : “clerk”, “id”: 3 } } Show Detail Now we will implement an API endpoint /:id which will be called using user ID and it will display the detail of the corresponding user. Add the following method in index.js file − app.get(”/:id”, function (req, res) { fs.readFile( __dirname + “/” + “users.json”, ”utf8”, function (err, data) { var users = JSON.parse( data ); var user = users[“user” + req.params.id] res.end( JSON.stringify(user)); }); }) In the Insomnia interface, enter http://localhost:5000/2 and send
Node.js – Buffers
Node.js – Buffers ”; Previous Next An object of buffer class in Node.js used to represent a fixed-length sequence of bytes. It stores raw data similar to an array of integers but corresponds to a raw memory allocation outside the V8 heap. The Buffer class in Node.js is a subclass of JavaScript”s Uint8Array class. Although an instance Buffer class is a global object, it is recommended that the class is explicitly referenced via an import or require statement. In earlier versions of Node.js, a buffer object is declared with new operator − var buf = new Buffer(10); A buffer object can also be created from a given array − var buf = new Buffer([10, 20, 30, 40, 50]); or from a given string − var buf = new Buffer(“Simply Easy Learning”, “utf-8″); However, the use of new keyword has now been deprecated. You need to use the following static methods to create a Buffer object − // Creates a zero-filled Buffer of length 10. const buf1 = Buffer.alloc(10); // Creates an uninitialized buffer of length 10. const buf2 = Buffer.allocUnsafe(10); // Creates a Buffer containing array const buf3 = Buffer.from([1, 2, 3]); // creates a buffer from string const buf4 = Buffer.from(”hello world”, ”utf8”); Static methods alloc() Allocates a new Buffer of specified size in bytes. Buffer.alloc(size[, fill[, encoding]]) Parameters size − The desired length of the new Buffer. fill − A value to pre-fill the new Buffer with. Default: 0. encoding − If fill is a string, this is its encoding. Default: ”utf8”. Example const buf = Buffer.alloc(5); console.log(buf); Output <Buffer 00 00 00 00 00> allocUnsafe() Creates an uninitialized buffer of specified size in bytes Buffer.allocUnsafe(size) Example const buf = Buffer.allocUnsafe(10); console.log(buf); buf.fill(”a”); console.log(buf); Output <Buffer 38 67 ff aa 32 56 00 00 08 00> <Buffer 61 61 61 61 61 61 61 61 61 61> from() Allocates a new Buffer using an array of bytes in the range 0 – 255. Example // Creates a new Buffer containing the UTF-8 bytes of the string ”buffer”. const buf1 = Buffer.from([0x62, 0x75, 0x66, 0x66, 0x65, 0x72]); console.log(buf1); // Creates a new Buffer from integer array. const buf2 = Buffer.from([10, 20, 30, 40, 50]); console.log(buf2); Output <Buffer 62 75 66 66 65 72> <Buffer 0a 14 1e 28 32> from() Creates a new Buffer containing string. Buffer.from(string[, encoding]) Parameters string <string> A string to encode. encoding <string> The encoding of string. Default: ”utf8”. The encoding parameter identifies the character encoding to be used when converting string into bytes. Example const buf1 = Buffer.from(”Hello World”); console.log(buf1); console.log(buf1.toString()); Output <Buffer 48 65 6c 6c 6f 20 57 6f 72 6c 64> Hello World compare() Compares a buffer object with other buffer object. compare(target[, targetStart[, targetEnd[, sourceStart[, sourceEnd]]]]) Parameters target − A Buffer with which to compare. targetStart − The offset within target at which to begin comparison. Default: 0. targetEnd − The offset within target at which to end comparison (not inclusive). Default: target.length. sourceStart − The offset within source buffer at which to begin comparison. Default: 0. sourceEnd − The offset within source buffer at which to end comparison (not inclusive). Default: buf.length. The function returns a number indicating whether the first buffer comes before, after, or is the same as the other buffer object in sort order. 0 is returned if second is the same as first 1 is returned if second buffer should come before the first when sorted. -1 is returned if second buffer should come after the first when sorted. Example var buffer1 = Buffer.from(”BALL”); var buffer2 = Buffer.from(”BAT”); var result = buffer1.compare(buffer2); if(result < 0) { console.log(buffer1 +” comes before ” + buffer2); } else if(result === 0) { console.log(buffer1 +” is same as ” + buffer2); } else { console.log(buffer1 +” comes after ” + buffer2); } Output BALL comes before BAT In the above example, change the two Buffer objects to − var buffer1 = Buffer.from(”CURE”); var buffer2 = Buffer.from(”CORE”); Output CURE comes after CORE copy() Copies data from a region of this buffer to a region in the target buffer even if the target memory region overlaps with the source. copy(target[, targetStart[, sourceStart[, sourceEnd]]]) Parameters target − A Buffer to copy into. targetStart − The offset within target at which to begin writing. Default: 0. sourceStart − The offset within buf from which to begin copying. Default: 0. sourceEnd − The offset within buf at which to stop copying (not inclusive). Default: buf.length. Example var buffer1 = Buffer.from(”Hello World”); var buffer2 = Buffer.allocUnsafe(buffer1.length); var result = buffer1.compare(buffer2); buffer1.copy(buffer2); console.log(“buffer2 content: ” + buffer2.toString()); Output buffer2 content: Hello World entries() returns an iterator of [index, byte] pairs from the contents of buffer object. Example var buf = Buffer.from(”Hello World”); for (const pair of buf.entries()) { console.log(pair); } Output [ 0, 72 ] [ 1, 101 ] [ 2, 108 ] [ 3, 108 ] [ 4, 111 ] [ 5, 32 ] [ 6, 87 ] [ 7, 111 ] [ 8, 114 ] [ 9, 108 ] [ 10, 100 ] slice() returns a new Buffer cropped by given indices buf.slice([start][, end]) Parameters start − Number, Optional, Default: 0 end − Number, Optional, Default: buffer.length Return Value Returns a new buffer, offset and cropped by the start (defaults to 0) and end (defaults to buffer.length) indexes. Negative indexes start from the end of the buffer. Example var buffer1 = Buffer.from(”TutorialsPoint”); //slicing a buffer var buffer2 = buffer1.slice(0,9); console.log(“buffer2 content: ” + buffer2.toString()); Output buffer2 content: Tutorials write() Writes string to a buffer at offset according to the specified encoding. write(string[, offset[, length]][, encoding]) Parameters string − String to write to buf. offset − Number of bytes to skip before starting to write string. Default: 0. length − Maximum number of bytes to write (written bytes will not exceed buf.length – offset). Default: buf.length – offset. encoding − The character encoding of string. Default: ”utf8”. The function returns the number of bytes written. Example const buf = Buffer.alloc(256); const