您现在的位置是:首页 > Javascript
当前栏目
[nodejs] nodejs开发个人博客(二)入口文件
2023-02-18 15:49:56 时间
错误处理中间件
定义错误处理中间件必须使用4个参数,否则会被作为普通中间件
/*错误处理器*/ application.use(function(err,req,res,next){ console.error(err.stack); res.status(500).send("代码出错了,错误信息:<br/>"+err.stack); }); /*404*/ application.use(function(req,res,next){ res.status(404).send("404页面被火星人挖走了"); });
创建文件结构
公共文件夹(common),控制器文件夹(controller),模型文件夹(model),视图文件夹(view),静态资源文件夹(static)
定义配置文件和函数文件并载入
配置文件common/config.js
/** * 公共配置文件 */ module.exports={ DB_HOST:'localhost', DB_NAME:'blog', DB_USER:'root', DB_PASS:'root', DB_PRE:'', APP_PORT:'8888' };
函数文件common/functions.js
/** * 公共函数文件 */ module.exports={ /*模拟php的date()函数*/ 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'));
路由级中间件
控制器分为两组home和admin
/*路由级中间件*/ application.use('/',require('./controller/home/index')); application.use('/admin',require('./controller/admin/index'));
相关文章
- 《JavaScript 模式》读书笔记(2)— 基本技巧3
- 《JavaScript 模式》读书笔记(2)— 基本技巧2
- 《JavaScript 模式》读书笔记(2)— 基本技巧1
- 《JavaScript 模式》读书笔记(1)— 简介
- js算法初窥07(算法复杂度)
- js算法初窥06(算法模式03-函数式编程)
- js算法初窥05(算法模式02-动态规划与贪心算法)
- js算法初窥04(算法模式01-递归)
- js算法初窥03(搜索及去重算法)
- js算法初窥02(排序算法02-归并、快速以及堆排序)
- js算法初窥01(排序算法01-冒泡、选择、插入)
- 用js来实现那些数据结构及算法—目录
- 用js来实现那些数据结构16(图02-图的遍历)
- 用js来实现那些数据结构15(图01)
- 用js来实现那些数据结构14(树02-AVL树)
- 用js来实现那些数据结构13(树01-二叉搜索树的实现)
- 用js来实现那些数据结构12(散列表)
- 用js来实现那些数据结构11(字典)
- 用js来实现那些数据结构10(集合02-集合的操作)
- 用js来实现那些数据结构09(集合01-集合的实现)