Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Sample Output HTML file, after connecting to localhost C localhost:8080 PENELOPE GUINESS NICK WAHLBERG ED CHASE JENNIFER DAVIS JOHNNY LOLLOBRIGIDA BETTE NICHOLSON GRACE MOSTEL
Sample Output HTML file, after connecting to localhost C localhost:8080 PENELOPE GUINESS NICK WAHLBERG ED CHASE JENNIFER DAVIS JOHNNY LOLLOBRIGIDA BETTE NICHOLSON GRACE MOSTEL Overview In this assignment, you will use the MySQL module to connect to a MySQL Server. The application will run a simple select statement, iterate through the resultset, and display the records in the form of a simple HTML page using Node.js. Database https://dev.mysql.com/doc/sakila/en/ https://dev.mysql.com/doc/sakila/en/sakila-structure.html Work on the JavaScript file 4. Create Global Variables 4.1. Create a new JavaScript file named sql Test.js located in the project folder. 4.2. Import the http module using the require keyword at the top of the file. Assign the reference to a global variable named http. Use let to declare the variable. 4.3. Import the mysql module using the require keyword at the top of the file. Assign the reference to a global variable named mysql. Use let to declare the variable. 4.4. Declare a variable called output, which will store the HTML response output. 5. Call the initializeDB() method. You will write it shortly! 6. Create a Node.js Server 6.1. Invoke the createServer method using your http object. It requires a callback method as an argument. Use method name processServerRequest (you will create the method shortly). Store the reference in a new global variable named httpServer. 6.2. Start the server by invoking its listen method. Configure the server to listen on port 8080. Login: host: cis425.cviulu0113xf.us-west-2.rds.amazonaws.com database: sakila port: 3306 user: reader password: Go+Sun+Devils! 2. Install the mysql package using NPM 2.1. Open a command prompt or terminal window and change the directory to your project folder. 2.2. Use npm install mysql to install the mysql package within your project folder. 7. Implement the method to connect to the database and perform other database tasks Function Name: initializeDB Parameters: none Return value: none Purpose: This function will use the MySQL module to connect to the database, run a query and terminate the connection. 6.1 Declare a variable named connectionString and assign to a JavaScript object. It should have the following name/value pairs. Ensure that you are using the JSON namevalue structure (there is no array, so there will be no square brackets). Name host Value cis425.cviulu0113xf.us-west-2.rds.amazonaws.com port 3306 database sakila reader user password Go+Sun+Devils! 6.2 6.3 6.4 6.5 Log the connection string to the console. Create a connection by invoking the createConnection method of the mysql object. Pass the connectionString to the method as an argument. createConnection will return a reference to the new connection object. Store it in a new variable named con. Log that you are going to connect to the database. Start the connection by invoking the connect method of the con object. Pass an inline function declaration as an argument. The function should accept one variable, err, as an argument. The following statements are the body of the inline function. The function is anonymous, i.e. it doesn't have a name. 6.6 Use an if statement to test if there was an error (using the err variable). If so, log the error and throw the err object, i.e. "throw err;". Create a new SQL query and store it in a sql query variable. Select first_name, last name from the actor table. 6.7 Log that you are connected to the database. 6.8 6.9 Execute the query by invoking the query method of the con object. Pass the sql query text as an argument. In addition, the query method expects a callback function as an argument. Pass a reference to the method process Result as the second argument. (You will write the method shortly). 6.10 Terminate the connection by calling the end method of the con object. 9 Implement the method to process one record of the resultset. Function Name: printActor Parameters: one: record Return value: none Purpose: This method will process one record of the sql resultset. For each record, it will construct a paragraph element which contains the first and last names of the actor. The following parts are the body of the method. 9.1 Using string concatenation, append to the existing output variable. Append a new paragraph tag which contains the first_name and last_name attributes of the record. Use strong tags to make the last name bold. This all can be done in one line of code! 10 Test your Program 10.1 Open a command prompt or terminal window. 10.1.1 On MacOS, you can easily open a terminal window which is already within the folder. 10.1.2 On Windows, change directory to work in the exercise folder. You can use the cd command and paste the full folder path. 10.2 Type node followed by the name of the js file to run the file. 10.3 Open Chrome and navigate to the local host server using port 8080. If your program works, it should connect to the Node.js script and execute the full script. The Node.js program should then send an HTML response back. You should then see the first and last names of all actors within individual paragraph elements.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Heres how you can solve the problem of connecting to a MySQL database from a Nodejs application and displaying actor information combining the insights from all five images 1 Install the MySQL Driver ...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started