本文為大家分享了nodejs個(gè)人博客開發(fā)的載入頁面,具體內(nèi)容如下
模板引擎
使用ejs作為我們博客的前端模板引擎,用來從json數(shù)據(jù)生成html字符串
安裝:npm install ejs -save
使用:入口文件中寫入下面代碼,定義/view/目錄為視圖目錄
/*模板引擎*/ application.set('views',__dirname+'/views');application.engine('.html',require("ejs").__express);application.set('view engine','html');首頁路由控制器
/*** 首頁控制器*/var router=express.Router();router.get('/',function(req,res,next){ /*渲染模板*/ res.render("home/index");});module.exports=router;此時(shí)會(huì)加載/view/home/index.html模板文件,瀏覽器里正常輸出
鏈接數(shù)據(jù)庫
入口文件index.js
/*鏈接數(shù)據(jù)庫*/ global.db=require("./model/db").getInstances();數(shù)據(jù)庫模型文件/model/db.js
/*** 數(shù)據(jù)庫操作類*/var db={ /*數(shù)據(jù)庫對(duì)象*/ db:null, /*構(gòu)造函數(shù)*/ getInstances:function(){ this.connectDatabase(); return this; }, /*鏈接數(shù)據(jù)庫*/ connectDatabase:function(){ var mysql=require('mysql'); var db=mysql.createConnection({ host:C.DB_HOST, user:C.DB_USER, password:C.DB_PASS, database:C.DB_NAME }); db.connect(); this.db=db; this.C=C; }, select:function(tableName,callback,where,field){ field=field ? field : '*'; var sql="select "+field+" from "+this.C.DB_PRE+tableName; if(where){ sql+=" where "+where; } this.db.query(sql,callback); }}module.exports=db;以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注