Katire
Katire

Reputation: 21

Can't print results to my console with NodeJS

I'm a newbie around here and of course, I am newbie also to the nodejs technology. I'm writing to you because I have a big problem for me, maybe a little problem for you, with the following:

I have this part of the code and I want when a user clicks on a button then the user should insert 3 values for him: UserId, GameId, Bookid. Then I want with the help of node js to print the results to my console. But i see from F12 that it says there is a problem with this line

loadnodejs.html:9 Uncaught ReferenceError: require is not defined --> var mysql = require('mysql');.

I've done the procedure from w3schools but nothing is showing. Can you help me to print the results on my console?

<html>
   <head>
      <script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
      <script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
      <script>
         function load3() {
         do{
         var selection = window.prompt("Give the User Id:", "Type a number!");
         if ( /^[0-9]+$/.test(selection)) {
         flag1=false;
         }
         }while(flag1!=false);

         var flag2 = true;
           do{
         var selection2 = window.prompt("Give the Book Id:", "Type a number!");
         if ( /^[0-9]+$/.test(selection2)) {
         flag2=false;
         }
         }while(flag2!=false);

         var flag3= true;
         do{
         var selection3 = window.prompt("Give the Game Id:", "Type a number!");
         if ( /^[0-9]+$/.test(selection3)) {
         flag3=false;
         }
         }while(flag3!=false);

         var mysql = require('mysql');
         var con = mysql.createConnection({
         host: "127.0.0.1",
         user: "root",
         password: "",
         database: "mysql3"
         });
         con.connect(function(err) {
         if (err) throw err;
         con.query("SELECT * FROM components", function (err, result) {
         if (err) throw err;
         console.log(result);
         });
         });
         }
      </script>
   </head>
   <body>
      <input type="button" value="Load" id="load" onclick="load3()" class="button12" />   
   </body>
</html>

Upvotes: 1

Views: 97

Answers (2)

Atharva Jawalkar
Atharva Jawalkar

Reputation: 186

Node.JS is a server-side technology, not a browser technology. Thus, Node-specific calls, like require(), do not work in the browser.

See [browserify]:http://browserify.org/ or webpack if you wish to serve browser-specific modules from Node.

Upvotes: 1

tbking
tbking

Reputation: 9056

Node.js is JavaScript which runs on backend. You have added Node.js code to an HTML file, which runs in browser. Browser doesn't support Node.js and hence the error.

Make an API call to server, and move Node.js code to server. I hope it makes sense to you.

Upvotes: 2

Related Questions