How to create a local server connection for a NW.JS local desktop application?

Viewed 597

I'm new to Node.JS and NW.JS and this is my first project, a Desktop App Demo. I made a basic form and I'm trying to execute a local app so I want turn my web server into a local serever connection to send the form data from my application to my databse in MySQL Workbench. Also if anyone has a suggestion for simplifying this code it'll be welcome.


*EDIT

Ok, apparently I don't need a server connection (neither the app.js nor the router.js) only the db.js for the database connection, all I have to do now is to turn my index.js into jQuery code. Can someone help me?

Also, I'm new to StackOverflow. Do I have to change the title? Any suggestion to improve this post?


Form submit code (index.js) The exports.save code won't insert the form data into the database.

var enviar = document.querySelector('.enviar');

enviar.addEventListener('click',function(){
    var fecha = document.getElementById('fecha').value;
    console.log(fecha);
    if(!fecha){
        alert("Error! Por favor, introdusca la fecha.");
    } else {
        
        const conexion = require('./db');

        exports.save = (req, res)=>{
            var aleatorio = Math.random();
            var alString = aleatorio.toString();
            var alStringFix = alString.substr(2,3);
        
            var idfecha = new Date();
            var numfecha = [
                idfecha.getMinutes(),
                idfecha.getSeconds(),
                idfecha.getMilliseconds()
            ];

            var numero = numfecha.join("")+parseInt(alStringFix);
            const tipo = req.body.tipo;
            const fecha = req.body.fecha+':'+idfecha.getSeconds()+'.'+idfecha.getMilliseconds();
            const comentario = req.body.comentario;

            console.log(numero+" - "+tipo+' - '+fecha+' - '+comentario);

            conexion.query('INSERT INTO cuentas_evento SET ?',
            {numero_evento: numero, tipo_evento: tipo, fecha_evento: fecha, comentario: comentario},
            (error, resultados)=>{
                if(error){
                    throw error;
                } else {
                    res.redirect('/');
                }
            });
        }
        alert("Formulario Enviado!");
        location.reload();
        return false;
    }
});

MySQL Connection (db.js)

const mysql = require('mysql');

const conexion = mysql.createConnection({
    host:       'localhost',
    database:   'test',
    user:       'root',
    password:   'mypassword'
});

conexion.connect(function(error){
    if (error){
        throw error;
    } else{
        console.log('Conexión Exitosa!');
    }
});

module.exports = conexion;

*Serever connection not needed

Router (router.js)

const express = require('express');
const router = express.Router();

const conexion = require('./db');

router.get('/', (req,res)=>{
    res.render('./index');
})

const crud = require('./crud');
router.post('/', crud.save);

module.exports = router;

APP (app.js)

const express = require('express');
const app = express();
const path = require('path');

app.use(express.urlencoded({extended:true}));
app.use(express.json());

app.get('/', function(req, res) {
    res.sendFile(path.join(__dirname, 'index.html'));
});

app.use(express.static('public'));
app.use('/', require('./router'));

app.listen(3000, ()=>{
    console.log('Servidor corriendo en http://localhost:3000');
});

1 Answers
Related