Broad Network


Making EMySQL API work with Node.js Web Server

Web Development Basics with ECMAScript and MySQL in Node.js – Part 2

Web Development with ECMAScript and MySQL in Node.js

Foreword: In this part of the series, I talk about the basic ECMAScript principles for interfacing MySQL Server.

By: Chrysanthus Date Published: 16 Jul 2016

Introduction

This is part 2 of my series, Web Development Basics with ECMAScript and MySQL in Node.js. In this part of the series, I talk about the basic ECMAScript principles for interfacing MySQL Server. You should have read the previous part of the series before reaching here, as this is a continuation.

Information of the web page at the client’s computer meant for the MySQL database has to be received by a script at the server first. It is this server script that will send the information to the database, which is also in the server host computer (everything being equal). Information flow in the reverse direction from the database passes through a script before reaching the client. In this series the scripts at the server are written in ECMAScript. Everything taught in this part of the series can be placed in one server script. In practice, some of the principles would be used in one script, while others would be used in other scripts. You can have more than one ECMAScript server script in an application (web development).

When the server script and the Database server are in the same computer, we say the script and the database have the same host called, localhost. In this series you will use localhost:8080 for the code samples. For your commercial project, if the script and the database server are in two different computers, then in the script code you will have to replace "localhost" (and port), with the domain name that has the database server.

Downloading
API stands for Application Programming Interface. This is software that is placed between two other software. The API facilitates communication between the two software. The API you will use in this series is called EMySQL. It is free! It is actually an ECMAScript API for MySQL, which works in Node.js

I will explain in this and the next section, what I have done to get my ECMAScript interpreter, the API, the Node Server, and MySQL server to work together. You will do the same or a similar thing to have your own four components, work together. The 4 components are installed in the same hard drive.

I use ECMAScript version 6 (version 2015), ECMAScript MySQL API version 1.01, Node Web Server and the Windows Operating System.

To download EMySQL, go to:

EMySQL API

Installing EMySQL
Installation is simple. Unzip the package. Copy all the contents to the directory that has the home page of your website. In my computer, the directory is c:/server. You can copy the contents to a different directory if you really know what you are doing.

MySQL
MySQL database server is free. To download, just search the web. The installation MySQL file I use is, mysql-5.5.19-win32.msi.

Installing MySQL Server
To install MySQL, I double clicked the downloaded file, mysql-5.5.19-win32.msi. I allowed the default settings. After the MySQL main installation, the MySQL Sever Instant Configuration Wizard Window opens presenting the second installation phase for MySQL. In this second phase, I chose or allowed the following settings in their different windows : Detailed Configuration, Developer machine, Multifunctional Database, Decision Support, Enable TCP/IP Networking, port 3306, Enable Strict Mode, Standard Character set of Latin1, Install as Windows Service-Server, Name of MySQL, Include Bin Directory in Windows Path, Modify Security Settings-Enter, Your Password.

For MySQL password, you will type the password of your choice. Allow your user name as “root”, which is for the database administrator.

To install the ECMAScript MySQL API, I unzipped the downloaded file, ECMAScript-MySQL-API.zip . I copied all the contents I saw into my computer’s c:\server directory, and that was it. The directory opened had the files, Mysql.pm and Mysqlcom.pl, and another directory, Mysql. The files Mysql.js and Mysqlcom.js plus the directory, Mysql with all its content, were copied to c:\server (see previous part of the series) in my personal computer.

Port
Any application in a computer in the Internet (network), has two important figures associated with it. One called the IP address or domain name identifies the computer in the network. The other called the port, identifies the application in the computer. In a computer, the MySQL server is an application. The databases it produces are products and parts of the application. The web server is also an application. The web pages and scripts it works with are components that integrate themselves to the application.

In my system the port for the MySQL server is, 3306; this is the default port.

Basic Server Script Coding
In this section I talk about the basic script coding, using the EMySQL API. This API is easy to install and easy to use (as you saw above). When the script at the server receives data from the client it sends it to a database or an email box. In this section I talk about the script working with the database. I will talk about the script working with email later.

The code begins with:

    const mysql = require('./server/Mysql.js');

where Mysql.js is the main API file (module). mysql is the object created by the module. It can have a different name.

You connect to the server as follows:

    con = new mysql.Connection("root", "secret", "localhost", 3306, callbackFn(){});

where 3306 is the MySQL port number; localhost is the domain name (here it is local host); secret is the password; and root is the MySQL user name (use root in this series).

con is the connection return object, a name of your choice. The name of the constructor is Connection(). Do not forget to type the new operator and the dot.

So, whether the script is in the same computer as the database server or not, you have to connect to the database server. To connect you need a username. Above, the user name is “root”, the database administrator. You need a password. Above, the password is secret; you have to replace this with your own. You need the domain name of the database server (not the web server). In this series, the server script and the database server are in the same computer. So the domain name is localhost. If they are not in the same server, then you need to type the domain name of the database server, in place of “localhost”.

You also need the port of the database server (not the web server). In this series the default port (3306) is used, which comes with the default MySQL installation.

I will talk about callbackFn(){} later.

API query()
After connecting, you can send a query as follows:

        con.query("USE test", callbackFn(){});

where con is the object of the Connection constructor.

Closing a Connection
Node.js responds rather asynchronously than synchronously. This means that when you issue a command (statement), it may take some small time before you see the result. Any connection (socket) opened must be closed. The statement to close the connection is:

    con.close();

The ECMAScript CGI
When data is sent from the client, as it arrives in the server computer, it meets your script first. It is up to you now to extract the data in whatever form it came in and send to the email box or database server. More on these in the following parts of the series!

That is it for this part of the series. We stop here and continue in the next part.

Chrys

Related Links

Web Development Basics with ECMAScript and MySQL
ECMAScript Validation of HTML Form Data
Web Live Text Chart Application using ECMAScript and MySQL
More Related Links
Node Mailsend
EMySQL API
Node.js Web Development Course
Major in Website Design
Low Level Programming - Writing ECMAScript Module
ECMAScript Course

BACK NEXT

Comments

Become the Writer's Follower
Send the Writer a Message