Manoj Kumar M
Manoj Kumar M

Reputation: 165

Node.js - can it be used for Desktop app development - MySQL, DataTable & File Open

Since last 3days, I am after this, not sure I understood its purpose properly - Node.js/Electron.

Few years back I had built a simple VB.net application - It connects to Mysql (contains a table of filename with path), shows the filenamesPath as rows in grid, upon double click, it opens the file.

Can I do such a thing in Node.js or Electron?.

1) I am able to make a js file with a button which can open a local file, in Node.js desktop app window (not browser). [https://www.codediesel.com/nodejs/how-to-open-various-desktop-applications-from-nodejs/ ].

2) Also I am able to view mySql table as html table in browser with localhost:port as well as the rows in console-log window [https://www.sitepoint.com/using-node-mysql-javascript-client/]

Is it possible to club both of these 2, Or Should I try something else. [As the rows are more than 100K, would need also Ajax]

EDITED: test.html

<html>
<head>
    <script>window.$ = window.jQuery = require('./js/jquery.js');</script>
    <meta http-equiv="Content-Security-Policy" content="script-src 'unsafe-inline';">
</head>
<body>
<h1>Electron MySQL Example</h1>
<div id="resultDiv"></div>
<div>
    <input type="button" id="action-btn" value="Retrieve 10 first rows in the database" />
    <table id="table" border="1">
        <tbody>

        </tbody>
    </table>    
</div>
<script>
    var mysql      = require('mysql');
    var connection = mysql.createConnection({
      host     : '10.251.198.2',
      user     : 'root',
      password : '',
      database : 'test'
    });

    connection.connect();
    var sql = 'SELECT `id`,`name` FROM `employees`';
    connection.query(sql, function (error, results, fields) {
     if (error) console.log(error.code);
     else {
         console.log(results);
         $('#resultDiv').text(results[0].name); //emp_name is column name in your database
     }
    });
    connection.end(); 
</script> 

<!---New --->
<script>
        var mysql = require('mysql');

        function el(selector) {
            return document.getElementById(selector);
        }

        el('action-btn').addEventListener('click', function(){
            // Get the mysql service
            getFirstTenRows(function(rows){
                var html = '';

                rows.forEach(function(row){
                    html += '<tr>';
                    html += '<td>';
                    html += row.id;
                    html += '</td>';
                    html += '<td>';
                    html += row.name;
                    html += '</td>';
                    html += '</tr>';
                    console.log(row);
                });

                document.querySelector('#table > tbody').innerHTML = html;
            });
        },false);

        function getFirstTenRows(callback){
            var mysql = require('mysql');

            // Add the credentials to access your database
            var connection = mysql.createConnection({
                host     : '10.251.198.2',
                user     : 'root',
                password : '',
                database : 'test'
            });

            // connect to mysql
            connection.connect(function(err) {
                // in case of error
                if(err){
                    console.log(err.code);
                    console.log(err.fatal);
                }
            });

            // Perform a query
            $query = 'SELECT `id`,`name` FROM `employees` LIMIT 10';

            connection.query($query, function(err, rows, fields) {
                if(err){
                    console.log("An error ocurred performing the query.");
                    console.log(err);
                    return;
                }

                callback(rows);

                console.log("Query succesfully executed");
            });

            // Close the connection
            connection.end(function(){
                // The connection has been closed
            });
        }
    </script>
</body> 
</html>

Index.js

const electron = require('electron');
const app = electron.app;

const path = require('path');
const url = require('url');

const BrowserWindow = electron.BrowserWindow;

var mainWindow;
app.on('ready',function(){
  mainWindow = new BrowserWindow({
      width: 1024, 
      height: 768,
      webPreferences: {
            nodeIntegration: true
        },
      //backgroundColor: '#2e2c29'
  });
  //mainWindow.loadURL('https://github.com');
   mainWindow.loadURL(url.format({
    pathname: path.join(__dirname, 'test.html'),
    protocol: 'file:',
    slashes: true
  }));

});

Upvotes: 1

Views: 1727

Answers (2)

ROOT
ROOT

Reputation: 11622

Sure, you can build desktop Apps nowadays in Node, in fact there is multible options that you can choose from:

All of these frameworks/technology allow you to write you App in Javascript and run it on Desktop platforms.

Upvotes: 0

Kiran
Kiran

Reputation: 89

You can use Electron to create Desktop app and connect to Mysql database. Here are couple of useful links.

https://ourcodeworld.com/articles/read/259/how-to-connect-to-a-mysql-database-in-electron-framework

https://github.com/techiediaries/electron-mysql-demo

Node JS is primarily used to create REST API, serve web pages from the server. You may create API in Node JS using Express/Restify which interacts with DB and the Electron app can consume this service. It depends on your requirement whether or not you want to have API layer.

Upvotes: 2

Related Questions