Node.js: How to extract information from the database and show in HTML pages

0

I've learned how to fetch information from the MySQL database, but I can not manipulate this data to expose it in HTML pages. The code that I will expose below I got from w3school:

var mysql = require('mysql');

var con = mysql.createConnection({
  host: "localhost",
  user: "yourusername",
  password: "yourpassword",
  database: "mydb"
});

con.connect(function(err) {
  if (err) throw err;
  con.query("SELECT * FROM customers", function (err, result, fields) {
    if (err) throw err;
    console.log(result);
  });
});

Pay attention to the part that I get the data, it is stored in the variable result, but you can not manipulate this information outside the scope of the function. How do I get this data and put the result in an HTML page? Is it possible to do this without a framework? If not, which framework to use?

    
asked by anonymous 20.08.2018 / 18:43

1 answer

0

Callbacks:

The data can not come out of the scope of this function since it represents a callback , that is, it is not executed in the order of the code, being called only when the data is loaded from the database. data.

  

To learn more about callbacks , I suggest reading this question .

Choose to use an existing framework:

To facilitate this process, many developers choose to use frameworks . Below I will quote you in my order of preference:

  • AdonisJs , an MVC framework, similar to Laravel and Ruby on Rails;
  • ExpressJS , a minimalist framework for Node.JS;
  • hapi.js , still minimal, but with some more features than Express.
  • 20.08.2018 / 20:12