本文為大家分享了nodejs個(gè)人博客開發(fā)的入口文件,具體內(nèi)容如下
錯(cuò)誤處理中間件
定義錯(cuò)誤處理中間件必須使用4個(gè)參數(shù),否則會(huì)被作為普通中間件
/*錯(cuò)誤處理器*/application.use(function(err,req,res,next){ console.error(err.stack); res.status(500).send("代碼出錯(cuò)了,錯(cuò)誤信息:<br/>"+err.stack);});/*404*/application.use(function(req,res,next){ res.status(404).send("404頁(yè)面被火星人挖走了");});創(chuàng)建文件結(jié)構(gòu)
公共文件夾(common),控制器文件夾(controller),模型文件夾(model),視圖文件夾(view),靜態(tài)資源文件夾(static)

定義配置文件和函數(shù)文件并載入
配置文件common/config.js
/*** 公共配置文件*/module.exports={ DB_HOST:'localhost', DB_NAME:'blog', DB_USER:'root', DB_PASS:'root', DB_PRE:'', APP_PORT:'8888'};函數(shù)文件common/functions.js
/*** 公共函數(shù)文件*/module.exports={ /*模擬php的date()函數(shù)*/ phpDate:function(formatStr,time){ var paramModel='ymdhis'; if(!formatStr) formatStr="y-m-d h:i:s"; if(time){ myDateTime=new Date(time*1000); }else{ myDateTime=new Date(); } var strTimeArr=[ myDateTime.getFullYear().toString(), (myDateTime.getMonth()+1).toString(), myDateTime.getDate().toString(), myDateTime.getHours().toString(), myDateTime.getMinutes().toString(), myDateTime.getSeconds().toString(), ]; for(var i=0;i<strTimeArr.length; i++){ formatStr=formatStr.replace(paramModel.charAt(i), strTimeArr[i]); } return formatStr; }};載入公共文件,定義資源文件
/*載入公共文件,定義資源文件*/global.C=require("./common/config");global.F=require("./common/functions"); application.use(express.static('public'));路由級(jí)中間件
控制器分為兩組home和admin
/*路由級(jí)中間件*/application.use('/',require('./controller/home/index'));application.use('/admin',require('./controller/admin/index'));以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注