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: nodejs
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 – MySQL Join
Node.js – MySQL Join ”; Previous Next In this chapter, you will learn how we can implement the JOIN query in a Node.js application on the tables in a MySQL database. A relational database consists of multiple related tables linking together using common columns, which are known as foreign key columns. MySQL supports JOIN query with which it is possible to extract and use data from more than one tables in a Node.js application. As in the earlier examples, we should call the mysql.query() method, and pass a SQL query string to mysql.query() method, the string should represent the MySQL JOIN query syntax. We shall use the following tables in this chapter − Members table CREATE TABLE members ( member_id INT AUTO_INCREMENT, name VARCHAR(100), PRIMARY KEY (member_id) ); INSERT INTO members(name) VALUES(”John”),(”Jane”),(”Mary”),(”David”),(”Amelia”); Data in members table − mysql> select * from members; +———–+——–+ | member_id | name | +———–+——–+ | 1 | John | | 2 | Jane | | 3 | Mary | | 4 | David | | 5 | Amelia | +———–+——–+ Committees table CREATE TABLE committees ( committee_id INT AUTO_INCREMENT, name VARCHAR(100), PRIMARY KEY (committee_id) ); INSERT INTO committees(name) VALUES(”John”),(”Mary”),(”Amelia”),(”Joe”); Data in Committees table − mysql> select * from committees; +————–+——–+ | committee_id | name | +————–+——–+ | 1 | John | | 2 | Mary | | 3 | Amelia | | 4 | Joe | +————–+——–+ Some members are committee members, and some are not. On the other hand, some committee members are in the members table, some are not. MySQL INNER JOIN The syntax of using the inner join clause in the SELECT statement is as follows − SELECT column_list FROM table_1 INNER JOIN table_2 ON join_condition; The inner join clause compares each row from the first table with every row from the second table. If values from both rows satisfy the join condition, a new row created in the resuleset whose column contains all columns of the two rows from both tables and includes this new row in the result set. In other words, the inner join clause includes only matching rows from both tables. If the join condition uses the equality operator (=) with the matching column name being same in both the tables, you can use the USING clause instead of ON clause. SELECT column_list FROM table_1 INNER JOIN table_2 USING (column_name); Example In the following Node.js code, we shall use the members and Committees tables and obtain their Inner Join. var mysql = require(”mysql”); var con = mysql.createConnection({ host: “localhost”, user: “root”, password: “mypassword”, database: “mydb” }); var qry =` SELECT m.member_id, m.name AS member, c.committee_id, c.name AS committee FROM members m INNER JOIN committees c ON c.name = m.name; `; con.connect(function (err) { if (err) throw err; console.log(“Connected!”); con.query(qry, function (err, results) { if (err) throw err; results.forEach((row) => { console.log(JSON.stringify(row)); }); }); con.end(); }); Output {“member_id”:1,”member”:”John”,”committee_id”:1,”committee”:”John”} {“member_id”:3,”member”:”Mary”,”committee_id”:2,”committee”:”Mary”} {“member_id”:5,”member”:”Amelia”,”committee_id”:3,”committee”:”Amelia”} MySQL LEFT JOIN Left join is similar to an inner join. When joining two tables using a left join, the left join selects data starting from the left table. For each row in the left table, the left join compares with every row in the right table. If the values in the two rows satisfy the join condition, the left join clause creates a new row whose columns contain all columns of the rows in both tables and includes this row in the result set. If the values in the two rows are not matched, the left join clause still creates a new row whose columns contain columns of the row in the left table and NULL for columns of the row in the right table. Hence, the left join selects all data from the left table whether there are matching rows exist in the right table or not. In case there are no matching rows from the right table found, the left join uses NULLs for columns of the row from the right table in the result set. The syntax of a left join clause is as follows − SELECT column_list FROM table_1 LEFT JOIN table_2 USING (column_name); Let us change the query string in out Node.js code to the following statement − Example var qry =` SELECT m.member_id, m.name AS member, c.committee_id, c.name AS committee FROM members m LEFT JOIN committees c USING(name); `; Output {“member_id”:1,”member”:”John”,”committee_id”:1,”committee”:”John”} {“member_id”:2,”member”:”Jane”,”committee_id”:null,”committee”:null} {“member_id”:3,”member”:”Mary”,”committee_id”:2,”committee”:”Mary”} {“member_id”:4,”member”:”David”,”committee_id”:null,”committee”:null} {“member_id”:5,”member”:”Amelia”,”committee_id”:3,”committee”:”Amelia”} The unmatched columns are filled with NULL data. To find members who are not the committee members, you add a WHERE clause and IS NULL operator. SELECT m.member_id, m.name AS member, c.committee_id, c.name AS committee FROM members m LEFT JOIN committees c USING(name) WHERE c.committee_id IS NULL; MySQL RIGHT JOIN The right join clause is similar to the left join clause except that the treatment of left and right tables is reversed. The right join starts selecting data from the right table instead of the left table. The right join clause selects all rows from the right table and matches rows in the left table. If a row from the right table does not have matching rows from the left table, the column of the left table will have NULL in the final result set. SELECT column_list FROM table_1 RIGHT JOIN table_2 USING (column_name); Let us modify the query string variable as follows − var qry =` SELECT m.member_id, m.name AS member, c.committee_id, c.name AS committee FROM members m RIGHT JOIN committees c on c.name = m.name; `; The result returns the RIGHT JOIN of two tables
Node.js – Event Emitter
Node.js – Event Emitter ”; Previous Next The Node.js API is based on an event-driven architecture. It includes the events module, which provides the capability to create and handle custom events. The event module contains EventEmitter class. The EventEmitter object emits named events. Such events call the listener functions. The Event Emitters have a very crucial role the Node.js ecosystem. Many objects in a Node emit events, for example, a net.Server object emits an event each time a peer connects to it, or a connection is closed. The fs.readStream object emits an event when the file is opened, closed, a read/write operation is performed. All objects which emit events are the instances of events.EventEmitter. Since the EvenEmitter class is defined in events module, we must include in the code with require statement. var events = require(”events”); To emit an event, we should declare an object of EventEmitter class. var eventEmitter = new events.EventEmitter(); When an EventEmitter instance faces any error, it emits an ”error” event. When a new listener is added, ”newListener” event is fired and when a listener is removed, ”removeListener” event is fired. Sr.No. Events & Description 1 newListener(event, listener) event − String: the event name listener − Function: the event handler function This event is emitted any time a listener is added. When this event is triggered, the listener may not yet have been added to the array of listeners for the event. 2 removeListener(event, listener) event − String The event name listener − Function The event handler function This event is emitted any time someone removes a listener. When this event is triggered, the listener may not yet have been removed from the array of listeners for the event. Following instance methods are defined in EventEmitter class − Sr.No. Events & Description 1 addListener(event, listener) Adds a listener at the end of the listeners array for the specified event. Returns emitter, so calls can be chained. 2 on(event, listener) Adds a listener at the end of the listeners array for the specified event. Same as addListener. 3 once(event, listener) Adds a one time listener to the event. This listener is invoked only the next time the event is fired, after which it is removed 4 removeListener(event, listener) Removes a listener from the listener array for the specified event. If any single listener has been added multiple times to the listener array for the specified event, then removeListener must be called multiple times to remove each instance. 5 removeAllListeners([event]) Removes all listeners, or those of the specified event. It”s not a good idea to remove listeners that were added elsewhere in the code, especially when it”s on an emitter that you didn”t create (e.g. sockets or file streams). 6 setMaxListeners(n) By default, EventEmitters will print a warning if more than 10 listeners are added for a particular event. Set to zero for unlimited. 7 listeners(event) Returns an array of listeners for the specified event. 8 emit(event, [arg1], [arg2], […]) Execute each of the listeners in order with the supplied arguments. Returns true if the event had listeners, false otherwise. 9 off(event, listener) Alias for removeListener Example Let us define two listener functions as below − var events = require(”events”); var eventEmitter = new events.EventEmitter(); // listener #1 var listner1 = function listner1() { console.log(”listner1 executed.”); } // listener #2 var listner2 = function listner2() { console.log(”listner2 executed.”); } Let us bind these listeners to a connection event. The first function listener1 is registered with addListener() method, while we use on() method to bind listener2. // Bind the connection event with the listner1 function eventEmitter.addListener(”connection”, listner1); // Bind the connection event with the listner2 function eventEmitter.on(”connection”, listner2); // Fire the connection event eventEmitter.emit(”connection”); When the connection event is fired with emit() method, the console shows the log message in the listeners as above listner1 executed. listner2 executed. Let us remove the listener2 callback from the connection event, and fire the connection event again. // Remove the binding of listner1 function eventEmitter.removeListener(”connection”, listner1); console.log(“Listner1 will not listen now.”); // Fire the connection event eventEmitter.emit(”connection”); The console now shows the following log − listner1 executed. listner2 executed. Listner1 will not listen now. listner2 executed. The EventEmitter class also has a listCount() method. It is a class method, that returns the number of listeners for a given event. const events = require(”events”); const myEmitter = new events.EventEmitter(); // listener #1 var listner1 = function listner1() { console.log(”listner1 executed.”); } // listener #2 var listner2 = function listner2() { console.log(”listner2 executed.”); } // Bind the connection event with the listner1 function myEmitter.addListener(”connection”, listner1); // Bind the connection event with the listner2 function myEmitter.on(”connection”, listner2); // Fire the connection event myEmitter.emit(”connection”); console.log(“Number of Listeners:” + myEmitter.listenerCount(”connection”)); Output listner1 executed. listner2 executed. Number of Listeners:2 Print Page Previous Next Advertisements ”;
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