-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
427 lines (378 loc) · 10.6 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
var port =process.env.PORT || 3000;
var express = require('express');
var http = require('http');
var path = require('path');
var multer = require('multer');
var mongoose=require('mongoose');
var app =express();
var Blog=require("./models/blog.js");
var Photo=require("./models/photo.js");
var Board=require("./models/board.js");
var Comment=require("./models/comment.js");
var _=require('underscore');
mongoose.connect("mongodb://localhost/tezml");
app.set('views','./jade/pages');//视图
app.set('view engine', 'jade');//模板引擎
app.use(express.static(path.join(__dirname, 'public')));
app.listen(port);
var bodyParser = require('body-parser');
var ueditor = require("ueditor");
var jqupload = require("jquery-file-upload-middleware");
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
console.log('端口号:'+port+'已启动');
//添加文章
app.use("/ueditor/ue/", ueditor(path.join(__dirname, 'public'), function(req, res, next) {
// ueditor 客户发起上传图片请求
if(req.query.action === 'uploadimage'){
var foo = req.ueditor;
var date = new Date();
var imgname = req.ueditor.filename;
var img_url = '/imageData/ueditor/';
res.ue_up(img_url); //你只要输入要保存的地址 。保存操作交给ueditor来做
}
// 客户端发起图片列表请求
else if (req.query.action === 'listimage'){
var dir_url = '/imageData/ueditor/';
res.ue_list(dir_url); // 客户端会列出 dir_url 目录下的所有图片
}
// 客户端发起其它请求
else {
res.setHeader('Content-Type', 'application/json');
res.redirect('/ueditor/ueditor.config.json')
}}));
//博客表头图片上传
app.use("/upload/blogTitleImg",function(req,res,next){
jqupload.fileHandler({
uploadDir:function(){
return __dirname+'/public/imageData/blogTitleImg/';
},
uploadUrl:function(){
return '/imageData/blogTitleImg/';
}
})(req,res,next)
});
//图片上传
app.use("/upload/Photo",function(req,res,next){
jqupload.fileHandler({
uploadDir: function () {
return __dirname + '/public/imageData/photo/';
},
uploadUrl: function () {
return '/imageData/photo/';
}
})(req,res,next)
});
//博客内容
app.post('/blog/content/',function(req,res){
var id=req.body._id;
Blog.findById(id,function(err,blog){
console.log(blog);
if(err){
console.log(err)
}
var json={inner:blog.inner};
res.send(JSON.stringify(json));
//res.writeHead(200,{"Content-Type":"text/plain","Access-Control-Allow-Origin":"http://localhost:3000"});
res.end();
});
});
//添加文章
app.post('/blog/add/',function(req,res){
//var id=req.body.id;
var blogObj=req.body.blog;
var _blog;
/* if(id !="undefined"){
Blog.findById(id,function(err,blog){
if(err){
console.log(err)
}
_blog= _.extend(blog,blogObj);
_blog.save(function(err,blog){
if(err){
console.log(err)
}
res.redirect('/inner/' + blog._id)
})
})
}else{*/
_blog= new Blog({
//id:blogObj.id,
title:blogObj.title,
outline:blogObj.outline,
titleImg:blogObj.titleImg,
createTime:blogObj.createTime,
inner:blogObj.inner,
timeOriginal:Date.now(),
length:0,
comtent:[]
});
_blog.save(function(err,blog){
if(err){
console.log(err)
}
console.log(blog);
var json={blog:blog};
res.send(JSON.stringify(json));
res.end();
});
//}
});
//添加类
app.post('/photo/addType/',function(req,res){
var photoObj=req.body;
var _photo;
_photo= new Photo({
type:photoObj.type,
img:[],
titleImg:""
});
_photo.save(function(err,photo){
if(err){
console.log(err)
}
var json={type:photo.type};
res.send(JSON.stringify(json));
res.end();
});
});
//提交给前台
app.post('/photo/addPhoto/',function(req,res){
var photoObj=req.body;
var type=photoObj.type;
var _photo;
Photo.findById(type,function(err,data){
for (var i = 0; i < photoObj.img.length; i++) {
data.img.push(photoObj.img[i])
}
data.titleImg=photoObj.titleImg;
_photo= new Photo(data);
/*res.send(JSON.stringify(data));
res.writeHead(200,{"Content-Type":"text/plain","Access-Control-Allow-Origin":"http://localhost:3000"});
res.end();*/
_photo.save(function(err,photo){
if(err){
console.log(err)
}
res.send(JSON.stringify(photo));
res.end();
});
});
});
//查询每个图片类型对应的所有信息
app.post('/photo/info/',function(req,res) {
var photoObj=req.body;
var type=photoObj.type;
Photo.findById(type,function(err,data){
res.send(JSON.stringify(data));
//res.writeHead(200,{"Content-Type":"text/plain","Access-Control-Allow-Origin":"http://localhost:3000"});
res.end();
});
});
//添加留言接口
app.post('/board/add/',function(req,res) {
var boardObj=req.body;
var _board;
_board= new Board({
message:boardObj.message,
nickName:boardObj.nickName,
img:boardObj.img
});
_board.save(function(err,data){
if(err){
console.log(err)
}
res.send(JSON.stringify(data));
//res.writeHead(200,{"Content-Type":"text/plain","Access-Control-Allow-Origin":"http://localhost:3000"});
res.end();
});
});
//添加评论
app.post('/comment/add/',function(req,res) {
var id=req.body.blogId;
var commentObj=req.body;
var _blog;
var type=req.body.type;
Blog.findById(id,function(err,blog){
if(err){
console.log(err)
}
//如果type是comment,走正常评论,如果是reply,走回复
if(type=="comment"){
var nBlog=blog;
var commentJson={
img:commentObj.img,
nickName:commentObj.nickName,
time:commentObj.time,
message:commentObj.message,
reply:[],
lou:commentObj.lou
};
nBlog.comment.push(commentJson);
_blog= _.extend(blog,nBlog);
_blog.save(function(err,blog){
if(err){
console.log(err)
}
res.send(JSON.stringify(blog));
res.end();
})
}else if(type=="reply"){
var lou=Number(req.body.lou)-1;
var Sblog=blog;
var commentJson={
img:commentObj.img,
nickName:commentObj.nickName,
time:commentObj.time,
message:commentObj.message
};
Sblog.comment[lou].reply.push(commentJson);
Sblog.save(function(err,blog){
if(err){
console.log(err)
}
console.log(blog);
res.send(JSON.stringify(blog));
res.end();
})
}
})
});
//图片分次加载
app.post('/photo/show/',function(req,res) {
var type=req.body.type;
var s=Number(req.body.s);
var now=Number(req.body.now);
Photo.findById(type,function(err,data){
if(err){
console.log(err)
}
var state=false;
if(now>=data.img.length){
state=true
}
var arr=[];
var all=s+now>=data.img.length?data.img.length:s+now;
for (var i = now; i < all; i++) {
arr.push(data.img[i])
}
var json={img:arr,now:now+s,state:state};
res.send(JSON.stringify(json));
//res.writeHead(200,{"Content-Type":"text/plain","Access-Control-Allow-Origin":"http://localhost:3000"});
res.end();
});
});
//路由
app.get('/',function(req,res){
res.render("index",{
classSeleced:"index"
})
});
app.get('/blog/',function(req,res){
Blog.fetch(function(err,blog){
if(err){
console.log(err)
}
res.render("blog",{
classSeleced:"blog",
blogs:blog
})
});
});
app.get('/gallery/',function(req,res){
Photo.fetch(function(err,data) {
if (err) {
console.log(err)
}
res.render("gallery", {
classSeleced: "gallery",
gallery: data
})
})
});
app.get('/board/',function(req,res){
Board.fetch(function(err,data) {
if (err) {
console.log(err)
}
console.log(data);
res.render("board",{
classSeleced:"board",
data:data
})
})
});
app.get('/contact/',function(req,res){
res.render("contact",{
classSeleced:"contact"
})
});
app.get('/inner/:id',function(req,res){
var id=req.params.id;
var nBlog;
Blog.findById(id,function(err,blog){
if(err){
console.log(err)
}
var _blog=blog;
_blog.length=_blog.length+1;
nBlog= _.extend(blog,_blog);
nBlog.save(function(err,blog){
if(err){
console.log(err)
}
console.log(blog);
res.render("inner",{
classSeleced:"Tezml",
data:blog
});
// res.writeHead(200,{"Content-Type":"text/plain","Access-Control-Allow-Origin":"http://localhost:3000"})
res.end();
});
})
});
app.get('/photo/:type',function(req,res){
var type=req.params.type;
Photo.findById(type,function(err,data){
if(err){
console.log(err)
}
res.render("photo",{
classSeleced:"Tezml",
data:data
})
});
});
app.get('/createImg/',function(req,res){
Photo.fetch(function(err,type){
if(err){
console.log(err)
}
res.render("createImg",{
photos:type
})
});
});
app.get('/createBlog/',function(req,res){
res.render("createBlog",{
})
});
//获取四个contect里的jade
app.get('/welcome/',function(req,res){
res.render("welcome",{
})
});
app.get('/AboutMe/',function(req,res){
res.render("AboutMe",{
})
});
app.get('/AboutWebsite/',function(req,res){
res.render("AboutWebsite",{
})
});
app.get('/ContactMe/',function(req,res){
res.render("ContactMe",{
})
});