我正在開發一個Web應用程序,用戶可以在網頁上輸入「設備ID」(我們有100個設備在生產領域使用每一個都有一個唯一的ID),用戶輸入的結果將被髮送到Node.js服務器,作爲回報將其存儲到一個變量中,並在SQL查詢中使用它來檢索關於該特定結果的結果設備,然後將結果顯示回客戶端網頁。通過Node.js發送客戶端表單輸入到服務器端數據庫查詢並將結果返回給客戶端
目前表單輸入功能尚未實現,即使我已經在html中編寫了表單代碼。
程序工作正常,因爲它是如果我的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();
我已經考慮了以下幾點: 1)使用一個onclick()從HTML代碼中的功能,如:
onclick="lookupSignID()
然後從HTML中包含一個外部JS文件包括lookupSignID()
功能,但我很快發現這只是執行客戶端功能,並不是我想要的。
2)AJAX僅適用於服務器自身生成新信息的情況,因此我無法使用AJAX,因爲用戶輸入設備ID以從中獲取信息。
3)可能使用POST/GET
任何人都可以請告知我應該採取什麼措施?如果解決方案(3)是最好的方式,我將如何去做這件事?它可以集成到我的現有代碼(如上所示),沒有太多的改變?
謝謝!
有,爲什麼你不」的理由使用express.js? – 2013-02-25 23:21:05