[nodejs] nodejs开发个人博客(二)入口文件

错误处理中间件

定义错误处理中间件必须使用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'));

 

posted @   唯一客服系统开发笔记  阅读(2055)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示
1
chat with us