本文为大家分享了nodejs个人博客开发的入口文件,具体内容如下
错误处理中间件
定义错误处理中间件必须使用四个参数,否则会被作为普通中间件
/*错误处理器*/application.use(函数(err,req,res,next){ console。错误(错误。堆栈);决议状况(500)。发送('代码出错了,错误信息: br/'错误。堆栈);});/*404*/application.use(函数(请求,决议,下一个){决议.现状(404)).发送(' 404页面被火星人挖走了');});创建文件结构
公共文件夹(常见),控制器文件夹(控制器),模型文件夹(型号),视图文件夹(视图),静态资源文件夹(静态)
定义配置文件和函数文件并载入
配置文件公共/config.js
/*** 公共配置文件*/模块。导出={ DB _ host : ' localhost ',DB_NAME:'blog ',DB_USER:'root ',DB_PASS:'root ',DB_PRE: ' ',APP _ port : ' 8888 ' };函数文件common/functions.js
/*** 公共函数文件*/module.exports={ /*模拟服务器端编程语言(专业超文本预处理器的缩写)的日期()函数*/phpdates :函数(formatStr,time){ var param model=' ymd his ';if(!format str)format str=' y-m-d h : I :s ';if(time){ myDateTime=新日期(时间* 1000);} else { myDateTime=new Date();} var strtimer=[my datetime。getfullyear().toString(),(myDateTime.getMonth() 1).toString()、myDateTime.getDate().toString()、myDateTime.getHours().toString()、myDateTime.getMinutes().toString()、myDateTime.getSeconds().toString(),];for(var I=0;isttimearr . lengthi){ format str=format str。替换(ParamMoDEL。charat(I),Strtimearr[I]);}返回formatStr } }载入公共文件,定义资源文件
/*载入公共文件,定义资源文件*/全局C=要求' ./common/config’);全球F=require(' ./common/functions’);应用程序。使用(快递。静态(' public ');路由级中间件
控制器分为两组家和管理
/*路由级中间件*/application.use('/',require(' ./控制器/主页/索引');application.use('/admin ',require(' ./controller/admin/index’;以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。