1 Star 0 Fork 779

被遗弃的娃/DoraCMS

forked from doramart/DoraCMS 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
app.js 4.53 KB
一键复制 编辑 原始数据 按行查看 历史

/*引用模块*/
var express = require('express');
var session = require('express-session');
var RedisStore = require('connect-redis')(session);
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var compression = require('compression');
var routes = require('./routes/index');
//用户相关功能
var users = require('./routes/users');
var admin = require('./routes/admin');
var content = require('./routes/content');
//验证器
var validat = require('./routes/validat');
//系统功能支持
var system = require('./routes/system');
//站点配置
var settings = require("./models/db/settings");
var siteFunc = require("./models/db/siteFunc");
//文件操作对象
var fs = require('fs');
//时间格式化
var moment = require('moment');
var filter = require('./util/filter');
/*模板引擎*/
var partials = require('express-partials');
/*实例化express对象*/
var app = express();
//ueditor注册
var ueditor = require('ueditor-nodejs');
app.use('/ueditor/ue', ueditor({//这里的/ueditor/ue是因为文件件重命名为了ueditor,如果没改名,那么应该是/ueditor版本号/ue
configFile: '/ueditor/jsp/config.json',//如果下载的是jsp的,就填写/ueditor/jsp/config.json
mode: 'local', //本地存储填写local
accessKey: '',//本地存储不填写,bcs填写
secrectKey: '',//本地存储不填写,bcs填写
staticPath: path.join(__dirname, 'public'), //一般固定的写法,静态资源的目录,如果是bcs,可以不填
dynamicPath: '/blogpicture' //动态目录,以/开头,bcs填写buckect名字,开头没有/.路径可以根据req动态变化,可以是一个函数,function(req) { return '/xx'} req.query.action是请求的行为,uploadimage表示上传图片,具体查看config.json.
}));
// view engine setup
//静态压缩
app.use(compression());
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(partials());
// uncomment after placing your favicon in /public
//app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(logger('dev'));
app.use(bodyParser.json({limit: '50mb'})); // 限制上传5M
app.use(bodyParser.urlencoded({ extended: false , limit: '50mb' }));
app.use(cookieParser(settings.session_secret));
//解决异步层次混乱问题
app.use(require('express-promise')());
app.use(session({
secret: settings.session_secret,
store: new RedisStore({
port: settings.redis_port,
host: settings.redis_host,
pass : settings.redis_psd,
ttl: 1800 // 过期时间
}),
resave: true,
saveUninitialized: true
}));
app.use(filter.authUser);
app.use(function(req, res, next){
// 针对注册会员
res.locals.logined = req.session.logined;
res.locals.userInfo = req.session.user;
// 针对管理员
res.locals.adminlogined = req.session.adminlogined;
res.locals.adminUserInfo = req.session.adminUserInfo;
// 指定站点域名
res.locals.myDomain = req.headers.host;
next();
});
//配置站点地图和robots抓取
app.get('/sitemap.xml',function(req, res, next) {
siteFunc.setDataForSiteMap(req, res);
});
app.get('/robots.txt',function(req, res, next) {
var stream=fs.createReadStream('./robots.txt',{flags:'r'});
stream.pipe(res);
});
//数据格式化
app.locals.myDateFormat = function(date){
moment.locale('zh-cn');
return moment(date).startOf('hour').fromNow();
};
app.use(express.static(path.join(__dirname, 'public')));
/*指定路由控制*/
app.use('/admin', validat);
app.use('/admin', admin);
app.use('/users', users);
app.use('/', routes);
app.use('/content', content);
app.use('/system',system);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
console.log(err);
res.render('web/public/do404', siteFunc.setDataForError(req, res, '找不到页面' ,err.message));
});
// error handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('web/public/do500', siteFunc.setDataForError(req, res, '出错啦!' ,err.message));
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('web/public/do500', siteFunc.setDataForError(req, res, '出错啦!' , err.message));
});
module.exports = app;
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/lgrgkzc/DoraCMS.git
git@gitee.com:lgrgkzc/DoraCMS.git
lgrgkzc
DoraCMS
DoraCMS
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385