標籤:style http os io 檔案 資料 ar cti
----------------------------------------------------------------------------------
/** --
* Created by Administrator on 14-8-16. --
*類比Ajax把資料從資料庫中調用出來/後台執行此代碼如下/ --
*/ --
var http = require(‘http‘);/建立伺服器的操作/ --
--
http.createServer(function(request,response){ --
response.writeHead(200,{‘Content-Type‘:‘text/plain;charset=utf-8‘}); --
getdb(request,response); --
}).listen(8080); --
--
var db = require(‘./select.js‘);/連結select.js檔案/ --
/下述代碼實現的是連結的伺服器和資料庫的操作/ --
function getdb(request,response){ --
// 類比Ajax強行輸出“請求”為:zzq --
request = ‘zzq‘; --
db.a(); --
db.b(request,response); --
db.c(); --
} --
--
------------------------------------------------------------ --
/** --
* Created by Administrator on 14-8-16. --
*/暴露介面/ --
*/ --
var mainline = require(‘./../shuju/mysql‘);/*載入資料庫*/ --
/*連結資料庫並且暴露資料連接埠*/ --
--
var conjs; --
exports.a =function(){ --
conjs = mainline.createConnection({ --
host:‘localhost‘, --
user:‘root‘, --
password:‘name‘, --
database:‘test‘ --
}); --
return conjs; --
} --
--
exports.b =function(request,response){ --
var sql ="select*from f08_4 where usen=?"; --
/查詢conjs資料庫中滿足條件的資料/ --
conjs.query(sql,[‘zzq‘],function(err,re){ --
if(err){ --
console.log(err); --
} --
else{console.log(re); --
response.write(""+re); --
/上述代碼錶示響應後返回資料/ --
response.end(); --
} --
}); --
} --
exports.c=function(){ --
conjs.end(); --
/上述語句執行的是結束資料的查詢/ --
} --
----------------------------------------------------------------------------------