简体   繁体   English

通过Node.js将客户端表单输入发送到服务器端数据库查询,并将结果返回给客户端

[英]Sending client side form input to server side database query via Node.js and return results to client

I am developing a web application where the user can type in a "device id" on the web page (we have 100's of devices out on the field in production use each with a unique ID), that result entered by the user will be sent to the Node.js server that in return will store it into a variable and use it in a SQL Query to retrieve results about that particular device from the database server and then display the results back to the client web page. 我正在开发一个Web应用程序,用户可以在网页上键入“设备ID”(生产现场使用的设备有100台,每台设备都有唯一的ID),用户输入的结果将被发送返回到Node.js服务器,作为回报,该服务器将其存储到变量中并在SQL查询中使用它,以从数据库服务器检索有关该特定设备的结果,然后将结果显示回客户端网页。

Currently the form input feature has not been implemented yet even though I've already coded the form code in html. 目前,即使我已经用html编码了表单代码,表单输入功能仍未实现。

The program works fine as it is if I were to manually change the DEVICE_ID to the device I wish to retrieve data from in the code but of course I want to be able to enter this on the client page instead of me having to change it in the server-side source code manually. 如果我将DEVICE_ID手动更改为希望从代码中检索数据的设备,则该程序可以正常工作,但是我当然希望能够在客户端页面上输入该设备,而不必在其中进行更改服务器端源代码手动。

"use strict";
var pg = require('pg').native;
var http = require('http');
var $ = require('cheerio');
var fs = require('fs');
var url = require('url');

var htmlString = fs.readFileSync('index.html').toString();
var parsedHTML = $.load(htmlString);

var dbUrl = "tcp://URL HERE/";

// The Sign-ID
var DEVICE_ID = '2001202';

// Create the http server
http.createServer(function (request, response) {
    var request = url.parse(request.url, true);
    var action = request.pathname;

    // Connect and query the database
    pg.connect(dbUrl, function(err, client) {
        // The complete sql query for everything that's needed for the app!
        client.query("SQL QUERY IS HERE" + DEVICE_ID + "etc..", 
            function (err, result) {
                // Remaining program code is here that performs DOM based 
                // manipulation to display results returned from the server
                // to the client side page.

                // Time to Render the document and output to the console
                console.log(parsedHTML.html());

                // Render the document and project onto browser
                response.end(parsedHTML.html());
            }
        ); // End client.query
    }); // End pg.connect
}).listen(8080); // End http.CreateServer

pg.end();

I've considered the following: 1) Use An OnClick() function from within the HTML code, like this: 我考虑了以下内容:1)从HTML代码中使用OnClick()函数,如下所示:

onclick="lookupSignID()

Then include an external JS file from within the HTML that includes the lookupSignID() function however I soon found out this is only performing client-side function and is not what I want. 然后在包含lookupSignID()函数的HTML中包含一个外部JS文件,但是我很快发现这仅在执行客户端功能,不是我想要的。

2) AJAX is only good for if the server is generating new information by itself, therefore I can't use AJAX since the user is entering the device ID to get information from it. 2)AJAX仅适用于服务器自行生成新信息的情况,因此我无法使用AJAX,因为用户正在输入设备ID来获取信息。

3) Possibly using POST/ GET 3)可能使用POST / GET

Can anyone please advise on what course of action I should take? 谁能建议我应该采取什么措施? If solution (3) is the best way how would I go about doing this? 如果解决方案(3)是最好的方法,我将如何去做呢? Can it be integrated into my existed code (shown above) without many changes? 可以将它集成到我现有的代码中(如上所示),而无需进行很多更改吗?

Thanks! 谢谢!

If you used jQuery on the client side with an AJAX POST function, and then on the server side, you have express.js, you could do this: 如果您在客户端使用带有AJAX POST函数的jQuery,然后在服务器端使用express.js,则可以执行以下操作:

app.post('/deviceid', function(req, res) {
   var deviceid = req.param('deviceid')
   console.log(deviceid)
   ...
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM