Present HTML Contents in Web Client, php, nodejs

home

part 1, in web client

table content from data

dog name dog color

code as below:

--------------------------------------------------------------- <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> <script> $(document).ready(function () { // after the dom is created $("#button1").click(function () { // 1. simulate data from network - REST GET request var data1 = '[{"dog_name": "Tairo", "dog_color": "brown"},'; var data2 = ' {"dog_name": "Emi", "dog_color": "white"}]'; var data = data1 + data2; // 2. parse the string into js object in json notation var jsonDogs = JSON.parse(data); // 3. present the data in a html table for (var i = 0; i < jsonDogs.length; i++) { //var dog = jsonDogs[r]; var dog = "<tr><td>" + jsonDogs[i]["dog_name"] + "</td>" + "<td>" + jsonDogs[i].dog_color + "</td></tr>"; $("#hTable").append(dog); }; }); }); </script> </head> ------------------------------------------------------------ <table id="hTable" style="margin-left:100px"> <tr> <td style="background-color: #dddddd">dog name</td> <td style="background-color: #dddddd">dog color</td> </tr> </table>----------------------------------------------------

description


part 2, in PHP web server

lab


part 3, in nodejs web server

Javascript is used to prepare the HTML contents, and return a page with the contents

section 3.1 get nodejs started ------------

section 3.2 using express -------

.

section 3. using embedded javascript as the view engine -------

code for index33.ejs

<!DOCTYPE html> <html> <head> <title>copy from w3c school</title> </head> <body> <h1>This is a Heading</h1> <p>This is a paragraph.</p> <p> the greeting is &nbsp;<%= greeting %> </p> <ul> <% drinks.forEach(function(drink) { %> <li><%= drink.name %> - <%= drink.drunkness %></li> <% }); %> </ul> </body> </html>

code for app33.js

 
            console.log("enter app3.js, test 1");

            var express = require("express");
            var app = express();

            app.set("view engine", "ejs");

            console.log("location 1");

            app.get("/", function(req, res){
                var drinkList = [
                    { name: 'draft beer', drunkness: 1 },
                    { name: 'Martini', drunkness: 2 },
                    { name: 'Scotch', drunkness: 3 }
                ];
                res.render("index.ejs", 
                           {
                               greeting: "hello world",
                               drinks: drinkList
                           }
                );
            });

            console.log("location 2");

            app.listen(3000, function(){
                console.log("the server is listening port 3000")
            });

            console.log("location 3");

            

section 3.4 mysql -------------

partial code for app34.js

var mysql = require("mysql");

app.get("/", function(req, res){
    var connection = mysql.createConnection({
          host: 'xxxxx.com',user: 'xxxxx',password: 'xxxxx',database :'xxxxx'       
    }); 
    connection.connect(function(err){
        if(!err) {console.log("db connected.");} else {console.log("db not connected.");};
    });  
    connection.query('SELECT * from dogs', function(err, rows, fields) {
            var dogArray = [];
            if (err) throw err;
            
            rows.forEach(function(dog){
                console.log("dog_name = " + dog.dog_name + "  dog_color = " + dog.dog_color);
                var little = {};
                little.dog_name = dog.dog_name;
                little.dog_color = dog.dog_color;
                console.log("little name = " + little.dog_name);
                dogArray.push(little);
            });
            connection.end();
            console.log("db disconnected");  
            console.log("dogArray.length = " + dogArray.length);
            res.render("index4.ejs", 
            {
                   dogs: dogArray,
                   greeting: "hello world"
            });
    });     
});
            

code for index34.ejs

<!DOCTYPE html> <html> <head> <title>copy from w3c school</title> <style> table, th, td { border: 1px solid black; border-collapse: collapse; } th, td { padding: 5px; text-align: left; } </style> </head> <body> <h1>ejs test</h1> <p>test 5</p> <p> the greeting is &nbsp;<%= greeting %> </p> <p><b><u>dog lists</u></b></p> <ul> <% dogs.forEach(function(dog){ %> <li><%= dog.dog_name %>,<%= dog.dog_color %></li> <% }); %> </ul> <p><b><u>dog table</u><b></p> <table> <tr style="background-color: #dddddd"> <td>dog name</td> <td>dog color</td> <tr> <% dogs.forEach(function(dog){ %> <tr> <td><%= dog.dog_name %></td> <td><%= dog.dog_color %></td> </tr> <% }); %> <table> </body> </html>