我需要在Javascript中插入一個字符串到MSSQL表中。如何在Node.JS中爲MSSQL創建預準備語句?
這是我到目前爲止有:
的Javascript:
var message = "It's a great day today!";
$.post('www.server.com/message='+message, function(response){
console.log(response);
});
的Node.js服務器:
//..... a bunch of code has been used to accept the HTTP request and get the message...
// for the purpose of this example, the message is asigned to 'NodeMsg'
var mssqldb = require("../core/mssql");
var NodeMsg = theMessageReceivedFromHTTPRequest;
function saveMessage(message) {
mssqldb.executeMssql("insert into messages (message, status) VALUES('"+message+"', 'new')", function (err) {
if (err) {
httpMsgs.show500(req, resp, err);
}//end if
else {
httpMsgs.sendJson(req, resp, 'success');
}//end else
});
};
mssql.js(node.js的文件):
var mssqldb = require("mssql");
var settings = require("../settings");
exports.executeMssql = function(sql, callback) {
var conn = new mssqldb.Connection(settings.mssqlConfig);
conn.connect()
.then(function(){
var req = new mssqldb.Request(conn);
req.query(sql)
.then(function (recordset) {
callback(recordset);
})
.catch(function(err){
console.log(err);
callback(null, err);
});
})
.catch(function(err){
console.log(err);
callback(null, err);
});
};//end executeMssql
摘要是怎麼回事的:
- 我定義一個消息字符串。 (請注意,它包含一個單引號)
- 我送該字符串Node.js的服務器
- 我使用Node.js的發送字符串到MSSQL
問題: 在瞭解了更多關於面向對象的程序之後,我意識到我執行插入的方式受到高度的關注,因爲它對SQL注入是開放的。而且,由於字符串中的單引號,代碼將在執行SQL查詢期間中斷。
解決方案: 根據我發現的大多數資源,解決方案是使用「準備好的語句」。
我的問題:
如何在地球上我轉換我已經做了,利用準備好的發言?我已經搜索網絡HOURS,我找不到一個很好的例子,說明如何使用Node.JS爲MSSQL(而不是MySQL)準備好的語句,這對初學者來說是易於理解的。Node.Js