gpt4 book ai didi

mysql - 想要将我的 web 应用程序转换为具有数据持久性的桌面应用程序

转载 作者:行者123 更新时间:2023-11-29 19:16:45 25 4
gpt4 key购买 nike

我创建了一个薪资 Web 应用程序,并使用 mysql 和 Node js 来构建它。问题是我不知道如何将其转换为桌面网络应用程序。我已经为此使用了node-mysql模块,所以我现在需要改变一些东西吗或者只是使用 Node WebKit 进行转换?

这到底是如何工作的?

这是我的 server.js 现在的样子。那么,我需要再次对代码进行更改吗?

var express = require('express');
var path = require('path');
var bodyParser = require('body-parser');
var app = express();
var mysql = require('mysql');
//var ejsLint=require('./server.js');
//ejsLint.lint('attendance-data.ejs', '-p');
var connection = mysql.createConnection({
host:'localhost',
user:'root',
password:'',
database:'employees',
multipleStatements:true
});

app.set('view engine','ejs');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

app.get('/', function (req,res){
res.render('index');
});
var daValue=227.30;
app.get('/da',function(req,res){
res.render('da');
});
app.post('/da-data',function(req,res){
daValue = Number(req.body.da);
var data = {da :daValue};
console.log('da is :',daValue);

res.render('da-data.ejs',data);
});

app.get('/add-employee',function(req,res){
res.render('add-employee');
});

app.post('/add-employee',function(req,res){
res.status(200).send();
console.log('employee '+req.body.name + ' added');
});

connection.connect(function(err){
if(err){
console.log('Error connecting to Db');
return;
}
console.log('Connection established');
});

app.post('/form',function (req,res){
//employee details from add-employee.html page
var name=req.body.name;
var designation = req.body.designation;
var pan = req.body.pan;
var aadhar = req.body.aadhar;
var bank = req.body.bank;
var basicSalary = req.body.salary;
var allowance = req.body.allowance;
var grossSalary = req.body.salarygross;
var esi = req.body.esi;
var uan = req.body.uan;


var details = {name:name,designation:designation,pan:pan,aadhar:aadhar,
bank:bank,basic_salary:basicSalary,other_allowances:allowance,gross_salary:grossSalary,esi:esi,uan:uan};
// for sending data as objects into database
// use {name of database column : name of the variable where it's value is stored}
// example {wages_rate is the name of database column : wagesRate is the variable where
// value is stored}

var query = connection.query('INSERT INTO employee_details SET ?',details,function(err,result){
if(err){
console.log(err);
}
console.log(query.sql);
});

res.status(200).send('employee ' + name + 'with salary of '+salary+ ' added');

});

app.get('/show',function (req,res){
connection.query('SELECT * FROM employee_details',function(err,rows){
if(err) throw err;

console.log('Data received');
console.log(rows);
var data ="Id "+"<strong>" +" name "+" designation "+" uan"+"</strong>";
for(var i=0;i<rows.length;i++){
data = data+"<br>" + (i+1)+". "+rows[i].name +" "+rows[i].designation+" "+rows[i].uan+"<br>";
}

res.send(data);
});
});

var rowsLength;
var salaryFromRow;
var salaryArr=[];
var allowanceFromRow;

var allowanceArr=[];
var designationArr=[];
app.get('/attendance',function (req,res){
connection.query('SELECT * FROM employee_details',function(err,rows){
if(err) {
throw err;
}else{
rowsLength = rows.length;

for(var i=0;i<rowsLength;i++){
salaryFromRow = rows[i].salary;
salaryArr.push(salaryFromRow);
allowanceFromRow = rows[i].allowance;
allowanceArr.push(allowanceFromRow);
designationArr.push(rows[i].designation);
console.log('designation is ',designationArr);
}





res.render('attendance',{rows:rows});
}


});
});


app.post('/attendance-data',function(req,res){
var uanFromHtml;

var nameFromHtml;

var daysPresent;
var attendance;
var nameForm;
var designation;
var monthFromHTML;
var t;
var realBasicSalary;
var realOtherAllowances;
var realGrossSalary;
var pfBasic;
var esiGross;
var pTax=0;
var netAmount;
var advance=0;
var finalData = [];
for(var i=1;i<=rowsLength;i++){
var dataArr = [];
var finalTable = [];

attendance = "attendance"+i;
t=i-1;
salaryForPerson = salaryArr[i];
allowanceForPerson = allowanceArr[i];
console.log('req.body is ', req.body);


daysPresent = Number(req.body[attendance]);
nameFromHtml = req.body.name[t];
var nameArr = req.body.name;
uanFromHtml = req.body.uan[t];
monthFromHTML = req.body.monthyear;

var uanArr = req.body.uan;
realBasicSalary = Math.ceil((req.body.basicsalary[t])*(daysPresent/30)) ;
realOtherAllowances = Math.ceil((req.body.otherallowance[t])*(daysPresent/30));
realGrossSalary = Math.ceil((req.body.grosssalary[t])*(daysPresent/30));
console.log('realBasicSalary is '+realBasicSalary+' and realGrossSalary is '+realGrossSalary+' and realOtherAllowances is '+ realOtherAllowances);
pfBasic = Math.ceil((12/100)*realBasicSalary);
esiGross = Math.ceil((1.75/100)*realGrossSalary);

if(realBasicSalary>10000 && realBasicSalary<=15000){
pTax = 110;
}else if(realBasicSalary>=15001 && realBasicSalary<=25000){
pTax = 130;
}else if(realBasicSalary>=25001 && realBasicSalary<=40000){
pTax = 150;
}else if(realBasicSalary>=40001){
pTax = 200;
}
netAmount = realGrossSalary - (pTax + pfBasic + esiGross + advance);

console.log('realGrossSalary is '+realGrossSalary + ' and realBasicSalary is '+realBasicSalary+
'pTax is '+ pTax+ 'pfBasic is '+pfBasic +'esiGross is '+esiGross);

console.log('namefromhtml is : ', nameFromHtml);
console.log('attendance is :',attendance);
console.log('days present is :',daysPresent);
console.log('monthyear is : ',monthFromHTML);
dataArr.push(monthFromHTML,uanFromHtml,nameFromHtml,daysPresent,realBasicSalary,realOtherAllowances,realGrossSalary,pTax);

finalData.push(dataArr);
/* uanArr.push(uanFromHtml);
nameArr.push(nameFromHtml);
daysArray.push(daysPresent);
*/

console.log('dataArr is : ',dataArr);
console.log('finalData is : ',finalData);

}
var attendanceData = {monthyear :monthFromHTML,rows:rowsLength,uanarr:uanArr,designationarr:designationArr,
namearr:nameArr,finaldata:finalData,realbasicsalary:realBasicSalary,realgrosssalary:realGrossSalary,ptax:pTax,advance:advance};

connection.query("INSERT INTO attendance_details(month_year,uan,name,days_present,real_basic_salary,other_allowances,gross_salary,ptax) VALUES ?",
[finalData], function(err) {
if (err){
var errors = err;
console.log(errors);
res.send(errors);
}else{


//put database query for inserting values here
res.render('attendance-data.ejs', attendanceData);
}

});

});

/*
app.get('/final',function(req,res){
connection.query('SELECT name,designation,salary,wages_rate FROM employee_details;SELECT uan,da,days_present,total_wages FROM attendance_details;',function(err,rows){
if(err){
console.error('MySQL — Error connecting: ' + err.stack);
}else{
var rowsNumber = rows.length;
console.log('rows is :',rows);
var nameFinal;
var designationFinal;
var salaryFinal;
var wagesrateFinal;
var uanFinal;
var daFinal;
var daysFinal;
var totalwagesFinal;
var nameFinalarr = [];
var designationFinalarr =[];
var salaryFinalarr = [];
var wagesrateFinalarr =[];
var uanFinalarr =[];
var daFinalarr =[];
var daysFinalarr = [];
var totalwagesFinalarr =[];



for(var i=0;i<rowsNumber;i++){
nameFinalarr.push(rows[i].name);
designationFinalarr.push(rows[i].designation);
salaryFinalarr.push(rows[i].salary);
wagesrateFinalarr.push(rows[i].wages_rate);
uanFinalarr.push(rows[i].uan);
daysFinalarr.push(rows[i].da);
daysFinalarr.push(rows[i].days_present);
totalwagesFinalarr.push(rows[i].total_wages);

}
console.log('nameFinalarr is :', nameFinalarr);
console.log('daysFinalarr is :', daysFinalarr);
}

res.render('final',{rows:rowsNumber,name:nameFinal,designation:designationFinal,salary:salaryFinal,wagesrate:wagesrateFinal,uan:uanFinal,da:daFinal,
days:daysFinal,
totalwages:totalwagesFinal});
});

});
*/

app.get('/select-month',function(req,res){
connection.query('SELECT month_year,name FROM attendance_details',function(err,rows){
if(err){
throw err;
}else{

var rowsLength = rows.length;
console.log('rows is ',rows);

res.render('select-month.ejs',{rows:rows});
}
});


});

app.get('/salary-sheet',function(req,res){
var month = req.query.selectpicker;
var employeeName = req.query.selectpicker2;
console.log('employeeName is ',employeeName);
connection.query('SELECT * FROM attendance_details WHERE month_year='+"'"+month+"' AND name='"+employeeName+"'",function(err,rows){
if(err){
throw err;
}else{
var rowsLength = rows.length;
console.log('rows is ',rows);
var uanarr=[];
var namearr=[];
var daysarr=[];
var realBasicSalaryarr=[];
var realOtherAllowancesarr=[];
var grossSalaryarr=[];
var ptaxarr=[];
var advance=0;
for(var i=0;i<rowsLength;i++){
uanarr.push(rows[i].uan);
namearr.push(rows[i].name);
daysarr.push(rows[i].days_present);
realBasicSalaryarr.push(rows[i].real_basic_salary);
realOtherAllowancesarr.push(rows[i].other_allowances);
grossSalaryarr.push(rows[i].gross_salary);
ptaxarr.push(rows[i].ptax);

}
console.log('realBasicSalaryarr is ',realBasicSalaryarr);
console.log('namearr is ',namearr);
res.render('salary-sheet.ejs',{advance:advance,rows:rows,monthyear:month,uanarr:uanarr,namearr:namearr,daysarr:daysarr,basicsalaryarr:realBasicSalaryarr,
realotherallowancesarr:realOtherAllowancesarr,realgrosssalaryarr:grossSalaryarr,ptaxarr:ptaxarr});
}
});


});



app.get('/add-company',function(req,res){
res.render('add-company.ejs');
});

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

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

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

var port=8080;
app.listen(8080,function(req,res){
console.log(`Payroll app listening on port ${port}!` );
});

您能告诉我如何将其转换为桌面应用程序吗?

最佳答案

为了使这项工作离线工作,换句话说,独立工作,您需要重新审视有关持久性的方法。我不会尝试将 MySQL 与应用程序捆绑在一起。您最好使用 Yonghoon Lee 建议的链接中的内容,因为您需要应用程序具有嵌入式数据库而不是外部数据库。

我已经成功地完成了类似的事情,尽管切换到 IndexDB 意味着我必须重写所有查询,因为它是 NoSQL 数据库。请抵制使用 Web SQL 数据库,它不是标准的,并且随时可能消失。

除此之外,我没有发现任何转换为​​ NWJS 应用程序存在明显问题的情况。您可以引用the official documentation有关如何开始的一些示例。

关于mysql - 想要将我的 web 应用程序转换为具有数据持久性的桌面应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42642887/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com