For a university project, I am tasked with creating a web application that is compatible with both desktop browsers and mobile devices. The technologies I will be using include HTML, CSS, JavaScript, Node.js, and MySQL. I have recently familiarized myself with node.js, which I understand to function like an apache server responding with an HTTP packet when a client makes a request. Additionally, I have implemented Node Express and integrated MySQL into my project. I have created a page with the .ejs extension that features a for loop iterating over a variable obtained from the database. Now, my questions are as follows: Who is responsible for executing the for loop? Is it the client or the server? Is the list/array of results retrieved from the MySQL query transmitted to the client for JavaScript to determine which rows to display, or does the server generate the HTML page and send it directly to the client?
This is the configuration of the node server:
var express = require('express');
var mysql = require('mysql');
var select;
var app = express();
app.set('view engine', 'ejs');
var con = mysql.createConnection({
host: "localhost",
user: "root",
password: "myPassword",
database: "test"
});
con.connect(function(err) {
if (err) throw err;
con.query("SELECT nome FROM prova", function (err, result, fields) {
if (err) throw err;
select = result;
});
});
app.get('/count/:number', function(req, res) {
res.render('page.ejs', {counter: req.params.number, names: select});
});
app.listen(8081);
This is the content of the requested page:
<html>
<head>
<title>Web Page Title</title>
</head>
<body><h1>Counting up to <%= counter %></h1>
<p><%
for (var i = 1; i <= counter; ++i) {
%>
<%= i %> ...
<% } %></p>
<p>Great, I'm done counting. Let me now randomly choose a name from those sent to me.
<%= names[0].nome %>
</p>
</body>
</html>