Skip to content

Commit 256683c

Browse files
committed
* merge cnodeclub to nodeclub;
* add more settings for custom site; * fixed upload.js not worked bug;
1 parent 8c4d01a commit 256683c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+722
-504
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
config.js
2+
node_modules

README.md

+43-30
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,47 @@
1-
##Node Club
2-
3-
###### 介绍
4-
Node Club 是用 **Node.js****MongoDb** 开发的新型社区软件,界面优雅,功能丰富,小巧迅速,已在Node.js 中文技术社区 [CNode](http://cnodejs.org) 得到应用,但你完全可以用它搭建自己的社区。
5-
6-
###### 安装部署
7-
// install node npm mongodb
8-
// run mongod
9-
cd nodeclub
10-
npm install ./
11-
cp config.default.js config.js
12-
// modify the config file as yours
13-
node app.js
1+
# nodeclub
2+
3+
基于nodejs的社区系统
4+
5+
## 介绍
6+
7+
Node Club 是用 **Node.js****MongoDb** 开发的新型社区软件,界面优雅,功能丰富,小巧迅速,
8+
已在Node.js 中文技术社区 [CNode](http://cnodejs.org) 得到应用,但你完全可以用它搭建自己的社区。
9+
10+
## 安装部署
11+
12+
```
13+
// install node npm mongodb
14+
// run mongod
15+
cd nodeclub
16+
npm install ./
17+
cp config.default.js config.js
18+
// modify the config file as yours
19+
node app.js
20+
```
1421

15-
###### 其它
22+
## 其它
23+
1624
小量修改了两个依赖模块:node-markdown,express
1725

18-
1.node-markdown/lib/markdown.js
19-
allowedTags 添加:
20-
21-
embed //支持 flash 视频
22-
table|thead|tbody|tr|td|th|caption //支持表格
23-
24-
25-
allowedAttributes 添加:
26-
27-
embed:'src|quality|width|height|align|allowScriptAccess|allowFullScreen|mode|type'
28-
table: 'class'
29-
30-
2.express/node_modules/connect/lib/middleware/csrf.js 添加:
26+
* node-markdown/lib/markdown.js
27+
28+
allowedTags 添加:
29+
30+
```
31+
embed //支持 flash 视频
32+
table|thead|tbody|tr|td|th|caption //支持表格
33+
```
3134

32-
if (req.xhr === true) return next();
33-
if (req.body.user_action && req.body.user_action == 'upload_image') return next();
34-
35+
allowedAttributes 添加:
36+
37+
```
38+
embed:'src|quality|width|height|align|allowScriptAccess|allowFullScreen|mode|type'
39+
table: 'class'
40+
```
41+
42+
* express/node_modules/connect/lib/middleware/csrf.js 添加:
43+
44+
```
45+
if (req.body && req.body.user_action === 'upload_image') return next();
46+
```
47+

app.js

100755100644
+31-15
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,62 @@
1-
var express = require('express'),
2-
routes = require('./routes'),
3-
config = require('./config').config;
1+
/*!
2+
* nodeclub - app.js
3+
*/
44

5-
var app = express.createServer();
5+
/**
6+
* Module dependencies.
7+
*/
8+
9+
var path = require('path');
10+
var express = require('express');
11+
var routes = require('./routes');
12+
var config = require('./config').config;
613

7-
var static_dir = __dirname+'/public';
14+
var app = express.createServer();
815

916
// configuration in all env
10-
app.configure(function(){
17+
app.configure(function() {
18+
var viewsRoot = path.join(__dirname, 'views');
1119
app.set('view engine', 'html');
12-
app.set('views', __dirname + '/views');
13-
app.register('.html',require('ejs'));
20+
app.set('views', viewsRoot);
21+
app.register('.html', require('ejs'));
1422
app.use(express.bodyParser());
1523
app.use(express.cookieParser());
1624
app.use(express.session({
17-
secret:config.session_secret,
25+
secret: config.session_secret,
1826
}));
1927
// custom middleware
2028
app.use(routes.auth_user);
2129
app.use(express.csrf());
30+
31+
// plugins
32+
var plugins = config.plugins || [];
33+
for (var i = 0, l = plugins.length; i < l; i++) {
34+
var p = plugins[i];
35+
app.use(require('./plugins/' + p[0])(p[1]));
36+
}
2237
});
2338

24-
//set static,dynamic helpers
39+
// set static, dynamic helpers
2540
app.helpers({
26-
config:config
41+
config: config
2742
});
2843
app.dynamicHelpers({
29-
csrf: function(req,res){
44+
csrf: function(req,res) {
3045
return req.session ? req.session._csrf : '';
3146
},
3247
});
3348

49+
var static_dir = path.join(__dirname, 'public');
3450
app.configure('development', function(){
3551
app.use(express.static(static_dir));
3652
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
3753
});
3854

3955
app.configure('production', function(){
40-
var one_year=1000*60*60*24*365;
41-
app.use(express.static(static_dir,{maxAge:one_year}));
56+
var maxAge = 3600000 * 24 * 30;
57+
app.use(express.static(static_dir, { maxAge: maxAge }));
4258
app.use(express.errorHandler());
43-
app.set('view cache',true);
59+
app.set('view cache', true);
4460
});
4561

4662
// routes

config.default.js

+21-4
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,23 @@
55
exports.config = {
66
name: 'Node Club',
77
description: 'Node Club 是用Node.js开发的社区软件',
8-
host: 'http://127.0.0.1', //host 结尾不要添加'/'
8+
version: '0.2.0',
9+
10+
// site settings
11+
site_headers: [
12+
'<meta name="author" content="慕远@TaoBao" />',
13+
],
14+
host: 'http://127.0.0.1', // host 结尾不要添加'/'
15+
site_logo: '', // default is `name`
16+
site_navs: [
17+
// [ path, title, [target=''] ]
18+
[ '/about', '关于' ],
19+
],
20+
921
db: 'mongodb://127.0.0.1/node_club',
1022
session_secret: 'node_club',
1123
auth_cookie_name: 'node_club',
1224
port: 80,
13-
version: '0.0.1',
1425

1526
// 话题列表显示的话题数量
1627
list_topic_count: 20,
@@ -26,8 +37,14 @@ exports.config = {
2637
//weibo app key
2738
weibo_key: 10000000,
2839

29-
// admins
3040
// admin 可删除话题,编辑标签,设某人为达人
31-
admins: {admin:true, obama:true}
41+
admins: { admin: true },
42+
43+
// [ [ plugin_name, options ], ... ]
44+
plugins: []
3245
};
3346

47+
var host = exports.config.host;
48+
if (host[host.length - 1] === '/') {
49+
exports.config.host = host.substring(0, host.length - 1)
50+
}

controllers/tools.js

+25
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,28 @@ var EventProxy = require('eventproxy').EventProxy;
1010
exports.run_site_tools = function(req,res,next){
1111
res.send('<h3>The White Castle</h3>');
1212
};
13+
14+
// exports.reset_data = function(req,res,next){
15+
// Topic.find({},function(err,topics){
16+
// for(var i=0; i<topics.length; i++){
17+
// var topic = topics[i];
18+
// if(topic){
19+
// topic.reply_count = 0;
20+
// topic.save();
21+
// }
22+
// }
23+
// });
24+
// res.end('end');
25+
// };
26+
27+
// exports.cal_data = function(req,res,next){
28+
// Reply.find({},function(err,replies){
29+
// for(var i=0; i<replies.length; i++){
30+
// var reply = replies[i];
31+
// if(reply.topic_id){
32+
// Topic.update({_id:reply.topic_id},{$inc:{reply_count:1}}).exec();
33+
// }
34+
// }
35+
// });
36+
// res.end('end');
37+
// };

controllers/upload.js

+14-14
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
var fs = require('fs');
22
var path = require('path');
3-
var config = require('../config').config;
43
var EventProxy = require('eventproxy').EventProxy;
54

65
var mod = require('express/node_modules/connect/node_modules/formidable');
7-
var upload_path = process.cwd() + '/public/user_data/images/';
6+
var upload_path = path.join(path.dirname(__dirname), 'public/user_data/images');
87
mod.IncomingForm.UPLOAD_DIR = upload_path;
98

10-
exports.upload_image = function(req,res,next) {
9+
exports.upload_image = function(req, res, next) {
1110
if (!req.session || !req.session.user) {
1211
res.send('forbidden!');
1312
return;
1413
}
15-
14+
var host = req.headers.host;
1615
var file = req.files.userfile;
1716
// sould use async
1817
if (file) {
@@ -22,34 +21,35 @@ exports.upload_image = function(req,res,next) {
2221
var time = new Date().getTime();
2322
var new_name = uid + time + ext;
2423
var proxy = new EventProxy();
24+
var userDir = path.join(upload_path, uid);
2525
function ensureDir() {
26-
path.exists(upload_path + uid, function(exists) {
26+
path.exists(userDir, function(exists) {
2727
if (!exists) {
28-
fs.mkdir(upload_path + uid, function(err) {
28+
fs.mkdir(userDir, function(err) {
2929
if (err) {
3030
return next(err);
3131
}
3232
proxy.emit('ensureDir');
33-
})
33+
});
3434
} else {
3535
proxy.emit('ensureDir');
3636
}
37-
})
37+
});
3838
}
3939
function moveImg() {
40-
var new_path = upload_path + uid +'/' +new_name;
41-
fd.rename(file.path, new_path, function(err) {
40+
var new_path = path.join(userDir, new_name);
41+
fs.rename(file.path, new_path, function(err) {
4242
if (err) {
4343
return next(err);
4444
}
45-
var url = config.host + '/user_data/images/'+ uid + '/' +new_name;
46-
res.json({status:'success',url: url});
47-
})
45+
var url = 'http://' + host + '/user_data/images/' + uid + '/' + new_name;
46+
res.json({ status: 'success', url: url });
47+
});
4848
}
4949
proxy.once('ensureDir', moveImg);
5050
ensureDir();
5151
} else {
52-
res.json({status:'failed'});
52+
res.json({ status: 'failed' });
5353
return;
5454
}
5555
};

0 commit comments

Comments
 (0)