This repository has been archived by the owner on Feb 6, 2024. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
817803b
commit e995593
Showing
307 changed files
with
66,664 additions
and
3,899 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,13 @@ | ||
# Logs | ||
logs | ||
*.log | ||
.DS_Store | ||
.idea | ||
Thumbs.db | ||
npm-debug.log | ||
install.lock | ||
config/database.js | ||
!.gitkeep | ||
|
||
# Runtime data | ||
pids | ||
*.pid | ||
*.seed | ||
|
||
# Directory for instrumented libs generated by jscoverage/JSCover | ||
lib-cov | ||
|
||
# Coverage directory used by tools like istanbul | ||
coverage | ||
|
||
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) | ||
.grunt | ||
|
||
# node-waf configuration | ||
.lock-wscript | ||
|
||
# Compiled binary addons (http://nodejs.org/api/addons.html) | ||
build/Release | ||
|
||
# Dependency directory | ||
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git | ||
node_modules | ||
|
||
Thumbs.db | ||
.DS_Store | ||
public | ||
.idea | ||
tmp/* | ||
logs/* | ||
public/media/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,123 +1,48 @@ | ||
var path = require('path'); | ||
var _ = require('lodash'); | ||
var express = require('express'); | ||
var compression = require('compression'); | ||
var exphbs = require('express-handlebars'); | ||
var favicon = require('serve-favicon'); | ||
var cookieParser = require('cookie-parser'); | ||
var bodyParser = require('body-parser'); | ||
var expressValidator = require('express-validator'); | ||
var validator = require('validator'); | ||
var session = require('express-session'); | ||
var mongoStore = require('connect-mongo')(session); | ||
var mongoose = require('./lib/database'); | ||
var logger = require('./lib/logger'); | ||
var router = require('./lib/route-map'); | ||
var validator = require('./lib/validator.lib'); | ||
var logger = require('./lib/logger.lib'); | ||
var router = require('./lib/route-map.lib'); | ||
var errors = require('./core/controllers/errors.controller').error; | ||
var sessionConfig = require('./config/session.config'); | ||
|
||
var app = express(); | ||
|
||
/** | ||
* 设置模板引擎 | ||
* 设置模板解析 | ||
*/ | ||
app.engine('.hbs', exphbs({ | ||
defaultLayout: 'main', | ||
extname: '.hbs' | ||
})); | ||
app.set('view engine', '.hbs'); | ||
|
||
/** | ||
* 中间件 | ||
*/ | ||
app.use(compression()); | ||
//app.use(favicon(__dirname + '/public/favicon.ico')); | ||
app.use(favicon(__dirname + '/public/favicon.ico')); | ||
app.use(logger.access()); | ||
app.use(bodyParser.json()); | ||
app.use(bodyParser.urlencoded({ extended: false })); | ||
app.use(expressValidator({ | ||
errorFormatter: function (param, message, value) { | ||
return { | ||
param: param, | ||
message: message, | ||
value: value | ||
}; | ||
}, | ||
customValidators: { | ||
isEmail: function (value) { | ||
return /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/.test(value); | ||
}, | ||
isString: function (value) { | ||
return typeof value === 'string'; | ||
}, | ||
isObject: function (value) { | ||
return (typeof value === 'object' && !Array.isArray(value)); | ||
}, | ||
isArray: function (value) { | ||
return Array.isArray(value); | ||
}, | ||
isBoolean: function (value) { | ||
return value === true || value === false; | ||
}, | ||
custom: function (value, callback) { | ||
if (typeof value !== 'undefined') { | ||
callback(value); | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
} | ||
})); | ||
app.use(validator()); | ||
app.use(cookieParser()); | ||
app.use(session({ | ||
secret: 'nodercms', | ||
name: 'nodercmsSid', | ||
cookie: { | ||
httpOnly: false | ||
}, | ||
store: new mongoStore({ | ||
mongooseConnection: mongoose.connection | ||
}), | ||
resave: false, | ||
saveUninitialized: false | ||
})); | ||
app.use(session(sessionConfig)); | ||
app.use(express.static(path.join(__dirname, 'public'))); | ||
|
||
/** | ||
* 转给Roter处理路由 | ||
* 转给 Roter 处理路由 | ||
*/ | ||
app.use('/', router); | ||
app.use(router); | ||
|
||
/** | ||
* 404处理程序 | ||
* 错误处理程序 | ||
*/ | ||
app.use(function(req, res, next) { | ||
var err = new Error('Not Found'); | ||
err.status = 404; | ||
next(err); | ||
}); | ||
app.use(errors); | ||
|
||
/** | ||
* 错误处理程序 | ||
* 导出 APP | ||
*/ | ||
if (app.get('env') === 'development') { | ||
app.use(function(err, req, res, next) { | ||
logger.system().error(err); | ||
|
||
res.status(err.status || 500); | ||
res.render('error', { | ||
message: err.message, | ||
error: err | ||
}); | ||
}); | ||
} | ||
|
||
app.use(function(err, req, res, next) { | ||
logger.system().error(err); | ||
|
||
res.status(err.status || 500); | ||
res.render('error', { | ||
message: err.message, | ||
error: {} | ||
}); | ||
}); | ||
|
||
module.exports = app; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,90 +1,84 @@ | ||
#!/usr/bin/env node | ||
|
||
/** | ||
* 读入主模块 | ||
*/ | ||
|
||
var async = require('async'); | ||
var http = require('http'); | ||
var hbs = require('express-hbs'); | ||
var app = require('../app'); | ||
var logger = require('../lib/logger'); | ||
var database = require('../lib/database.lib'); | ||
var logger = require('../lib/logger.lib'); | ||
var port = require('../lib/port.lib.js')(); | ||
var themes = require('../lib/themes.lib'); | ||
var installService = require('../core/services/install.service'); | ||
|
||
/** | ||
* 设置端口 | ||
*/ | ||
|
||
var port = normalizePort(process.env.PORT || '3000'); | ||
app.set('port', port); | ||
|
||
/** | ||
* 创建 HTTP 服务器 | ||
*/ | ||
|
||
var server = http.createServer(app); | ||
|
||
/** | ||
* 监听端口 | ||
*/ | ||
|
||
server.listen(port); | ||
server.on('error', onError); | ||
server.on('listening', onListening); | ||
|
||
/** | ||
* 格式化端口 | ||
* 启动服务器 | ||
*/ | ||
async.waterfall([ | ||
// 检查安装锁 | ||
function (callback) { | ||
installService.status(function (err, hasInstall) { | ||
if (err) return callback(err); | ||
|
||
callback(null, hasInstall); | ||
}); | ||
}, | ||
function (hasInstall, callback) { | ||
if (!hasInstall) { | ||
return callback(); | ||
} | ||
|
||
async.waterfall([ | ||
// 连接数据库 | ||
function (callback) { | ||
database.connect(function (err) { callback(err) }); | ||
}, | ||
// 初始化主题 | ||
function (callback) { | ||
themes.init(app, function (err) { callback(err) }); | ||
} | ||
], callback); | ||
} | ||
], function (err) { | ||
if (err) throw err; | ||
|
||
function normalizePort(val) { | ||
var port = parseInt(val, 10); | ||
//创建 HTTP 服务器 | ||
var server = http.createServer(app); | ||
|
||
if (isNaN(port)) { | ||
// named pipe | ||
return val; | ||
} | ||
//监听端口 | ||
server.listen(port); | ||
|
||
if (port >= 0) { | ||
// port number | ||
return port; | ||
} | ||
server.on('error', function (error) { | ||
if (error.syscall !== 'listen') throw error; | ||
|
||
return false; | ||
} | ||
var bind = typeof port === 'string' ? 'pipe ' + port : 'port ' + port; | ||
|
||
/** | ||
* 监听端口错误 | ||
*/ | ||
// handle specific listen errors with friendly messages | ||
switch (error.code) { | ||
case 'EACCES': | ||
logger.system().error(bind + ' 需要有更高的权限'); | ||
process.exit(1); | ||
|
||
function onError(error) { | ||
if (error.syscall !== 'listen') { | ||
throw error; | ||
} | ||
break; | ||
case 'EADDRINUSE': | ||
logger.system().error(bind + ' 已被使用'); | ||
process.exit(1); | ||
|
||
var bind = typeof port === 'string' | ||
? 'pipe ' + port | ||
: 'port ' + port; | ||
break; | ||
default: | ||
throw error; | ||
} | ||
}); | ||
|
||
// handle specific listen errors with friendly messages | ||
switch (error.code) { | ||
case 'EACCES': | ||
logger.system().error(bind + ' 需要有更高的权限'); | ||
process.exit(1); | ||
break; | ||
case 'EADDRINUSE': | ||
logger.system().error(bind + ' 已被使用'); | ||
process.exit(1); | ||
break; | ||
default: | ||
throw error; | ||
} | ||
} | ||
server.on('listening', function () { | ||
var addr = server.address(); | ||
|
||
/** | ||
* 监听端口事件 | ||
*/ | ||
var bind = typeof addr === 'string' ? 'pipe ' + addr : 'port ' + addr.port; | ||
|
||
function onListening() { | ||
var addr = server.address(); | ||
var bind = typeof addr === 'string' | ||
? 'pipe ' + addr | ||
: 'port ' + addr.port; | ||
logger.system().info('正在监听 ' + bind); | ||
} | ||
logger.system().info('正在监听 ' + bind); | ||
}); | ||
}); |
Oops, something went wrong.