-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
531 lines (473 loc) · 14.7 KB
/
index.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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
const http = require('http');
const fs = require('fs');
const path = require('path');
const url = require('url');
const routerMime = require('./router_mime.js');
const querystring = require('querystring');
const hostname = '127.0.0.1';
const port= 9999;
const mainUrl = 'static/index.html';
const server = http.createServer((req,res)=>{
let theUrl = url.parse(req.url);
// console.log('theUrl',theUrl);
let filePath = path.join(__dirname,'/static/',theUrl.path);
filePath = filePath.split('?')[0];
if(theUrl.query==null || theUrl.path.indexOf('/api')<0){
//无参数,返回html页面
if (theUrl.path =='/'){
filePath = path.join(__dirname,'static/index.html');
}
// console.log('filePath',filePath);
let filelas = '.'+filePath.match(/\.(\w+)$/)[1];
// console.log('filelas',filelas);
if (routerMime[filelas]) {
// console.log('routerMime',routerMime[filelas]);
res.writeHead(200, {
"Content-Type": routerMime[filelas]
});
var stream = fs.createReadStream(filePath);
stream.pipe(res);
stream.on('end',function(){
res.end();
});
}
}
else
{
//有参数,处理ajax请求
let getReq = querystring.parse(theUrl.query);
console.log('getReq',getReq);
let json = {};
//判断是什么接口
let MongoClient = require('mongodb').MongoClient;
var ObjectID = require('mongodb').ObjectID
let dbUrl = 'mongodb://127.0.0.1:27017/blog';
MongoClient.connect(dbUrl,(err,db)=>{
if(err){
console.log('err',err);
return false;
}
//查看访客数接口
if (req.url.indexOf('getvisit')>=0) {
if(getReq['main']){
//查看主页的访客数据
let cAppInfo = db.collection('app_info');
cAppInfo.find({}).toArray((err,data)=>{
// console.log('res-end',data);
// console.log('callback-data',data);
let json = JSON.stringify(data[0]);
// console.log('json',json);
res.writeHead(200, {});
res.end(json);
db.close();
});
}
}
//设置访客数接口
else if (req.url.indexOf('setvisit')>=0) {
if(getReq['main']){
//查看主页的访客数据
let cAppInfo = db.collection('app_info');
cAppInfo.find({}).toArray((err,data)=>{
// console.log('res-end',data);
// let json = JSON.stringify(data[0]);
let json = data[0];
// console.log('json',json);
var num =json.visitnum;
// num++;
// console.log('num', num);
var newnum = parseInt(num)+1;
// console.log('newnum',newnum);
// cAppInfo.updateOne({a:1}, {$set:{b:2}});
cAppInfo.update({visitnum:num},{$set:{visitnum:newnum}},(err,_res)=>{
// console.log('update',_res);
if (err) {
console.log("Error:" + err);
res.writeHead(300, {});
res.end('err');
}else{
res.writeHead(200, {});
res.end('ok');
}
db.close();
});
});
}
}
//获取文章详情接口/增加阅读数
else if (req.url.indexOf('blogdetail')>=0){
var sBlogid = getReq['blogid'];
if(sBlogid){
//传了blogid这个字段
let cBlogTb = db.collection('blog_tb');
// console.log('cBlogTb',cBlogTb);
findOneData(cBlogTb,{_id:ObjectID(sBlogid)},(data)=>{
// console.log('findOneData',data);
//更新阅读数,增加1
let nReadnum = parseInt(data.readnum)+1;
cBlogTb.update({_id:ObjectID(sBlogid)},{$set:{readnum:nReadnum}},(err,_res)=>{
console.log('nReadnum',nReadnum);
});
//返回详情给前端
let json = JSON.stringify(data);
// console.log('findOneJson',json);
res.writeHead(200, {});
res.end(json);
db.close();
});
function findOneData(collection,qurery,callback){
collection.findOne(qurery,(err,r)=>{
if (typeof callback =='function') {
callback(r);
}
});
}
}else{
//404 没有这篇文章
}
}
//获取文章列表接口
else if (req.url.indexOf('bloglist')>=0){
if(getReq['page']){
//判断是否存在分类id
let nTypeid = -1;
if(getReq['typeid']){
nTypeid = getReq['typeid'];
}
// console.log('nTypeid',nTypeid);
let jQuery = {};
if (nTypeid!=-1) {
jQuery["typeid"]=parseInt(nTypeid);
// jQuery = {typeid:nTypeid};这个做法是错误的
}
// console.log('jQuery',jQuery);
let nPage = parseInt(getReq['page']);
let nRows = parseInt(getReq['rows']);
// console.log('nRows',nRows);
let nSkip = (nPage-1)*nRows;
// console.log('nSkip',nSkip);
//传了blogid这个字段
let cBlogTb = db.collection('blog_tb');
cBlogTb.find(jQuery).skip(nSkip).limit(nRows).toArray((err,data)=>{
// console.log('findData',data);
let json = JSON.stringify(data);
//计算总共有多少条数据,返回页码给total
cBlogTb.count(jQuery,(err,count)=>{
let total = Math.ceil(count/10);
json = `{"total":${total},"data":${json}}`;
// console.log('findJson',json);
res.writeHead(200, {});
res.end(json);
db.close();
});
});
// function findData(collection,query,limit,callback){
// collection.find(query,limit).toArray((err,r)=>{
// if (typeof callback =='function') {
// callback(r);
// }
// });
// }
}else{
//404 没有这篇文章
}
}
//添加留言内容接口
else if (req.url.indexOf('setmsg')>=0) {
if(getReq['content']){
let jData = {};
jData['userid'] = getReq['userid'];
jData['name'] = getReq['name'];
jData['contact'] = getReq['contact'];
jData['content'] = getReq['content'];
jData['createtime'] = new Date();
jData['colnum'] = 0;
let cMessage = db.collection('message_tb');
cMessage.insertOne(jData,(err,_res)=>{
// console.log('insertOne',_res);
if (err) {
console.log("Error:" + err);
res.writeHead(300, {});
res.end('err');
}else{
console.log("cMessage:" ,'ok');
res.writeHead(200, {});
res.end('ok');
}
db.close();
});
}
}
//获取留言板列表接口
else if (req.url.indexOf('msglist')>=0){
if(getReq['page']){
// console.log('jQuery',jQuery);
let nPage = parseInt(getReq['page']);
let nRows = parseInt(getReq['rows']);
// console.log('nRows',nRows);
let nSkip = (nPage-1)*nRows;
// console.log('nSkip',nSkip);
//传了blogid这个字段
let cBlogTb = db.collection('message_tb');
cBlogTb.find({}).skip(nSkip).limit(nRows).toArray((err,data)=>{
// console.log('findData',data);
let json = JSON.stringify(data);
//计算总共有多少条数据,返回页码给total
cBlogTb.count({},(err,count)=>{
let total = Math.ceil(count/10);
json = `{"total":${total},"data":${json}}`;
// console.log('findJson',json);
res.writeHead(200, {});
res.end(json);
db.close();
});
});
// function findData(collection,query,limit,callback){
// collection.find(query,limit).toArray((err,r)=>{
// if (typeof callback =='function') {
// callback(r);
// }
// });
// }
}else{
//404 没有这篇文章
}
}
//添加博客文章接口
else if (req.url.indexOf('publish')>=0) {
if(getReq['detail']){
let jData = {};
jData['tittle'] = getReq['tittle'];
jData['brief'] = getReq['brief'];
jData['autor'] = getReq['autor'];
jData['detail'] = getReq['detail'];
jData['typeid'] = parseInt(getReq['typeid']);
jData['typename'] = getReq['typename'];
jData['createtime'] = new Date();
jData['updatetime'] = new Date();
jData['readnum'] = 0;
jData['comtnum'] = 0;
jData['colnum'] = 0;
let cBlogTb = db.collection('blog_tb');
cBlogTb.insertOne(jData,(err,_res)=>{
// console.log('insertOne',_res);
if (err) {
console.log("Error:" + err);
res.writeHead(300, {});
res.end('err');
}else{
// console.log("cBlogTb:" ,'ok');
res.writeHead(200, {});
res.end('ok');
}
db.close();
});
}
}
//添加用户注册接口
else if (req.url.indexOf('reg')>=0) {
if(getReq['username']){
let jData = {};
jData['username'] = getReq['username'];
let cUserTb = db.collection('user_tb');
//先查询//todo
cUserTb.count(jData,(err,count)=>{
console.log('cUserTb.count',count);
if (count>0) {
//存在用户名
let json = '{"code":101,"status":0,"msg":"已存在该用户"}';
res.writeHead(200, {});
res.end(json);
db.close();
}else{
jData['password'] = getReq['password'];
jData['isAdmin'] = getReq['isAdmin'];
jData['createtime'] = new Date();
//插入数据
cUserTb.insertOne(jData,(err,_res)=>{
// console.log('insertOne',_res);
let json = '{}';
if (err) {
// console.log("Error:" + err);
json = '{"code":102,"status":0,"msg":"数据查询出错"}';
res.writeHead(200, {});
res.end(json);
}else{
// console.log("cBlogTb:" ,'ok');
json = '{"code":100,"status":1,"msg":"注册成功"}';
res.writeHead(200, {});
res.end(json);
}
db.close();
});
}
});
}
}
//用户登录接口
else if (req.url.indexOf('login')>=0){
// console.log('login',getReq['username']);
if(getReq['username']){
//传了blogid这个字段
// console.log('login','login');
let cUserTb = db.collection('user_tb');
// console.log('cUserTb',cUserTb);
let query = {};
query["username"] = getReq['username'];
findOneData(cUserTb,query,(data)=>{
// console.log('findOneData',data);
// let json = JSON.stringify(data);
// console.log('json',json);
// console.log("getReq['password']",getReq['password']);
// console.log('json.password',data.password);
if (getReq['password']==data.password) {
//登录成功
// json = '{"code":100,"status":1,"msg":"登录成功"}';
json = `{"code":100,"status":1,"msg":"登录成功","userid":"${data._id}"}`;
// console.log('json',json);
res.end(json);
res.writeHead(200, {});
}else{
json = '{"code":102,"status":0,"msg":"密码错误"}';
res.writeHead(200, {});
res.end(json);
}
// console.log('findOneJson',json);
res.end(json);
db.close();
});
function findOneData(collection,qurery,callback){
collection.findOne(qurery,(err,r)=>{
if (typeof callback =='function') {
callback(r);
}
});
}
}else{
//404 没有这篇文章
}
}
//设置博客文章的参数接口 点赞
else if (req.url.indexOf('addblogcolt')>=0) {
if(getReq['blogid']){
let cBlogTb = db.collection('blog_tb');
let blogid = getReq['blogid'];
cBlogTb.findOne({_id:ObjectID(blogid)},(err,data)=>{
console.log('findOneData',data);
let nColnum = parseInt(data.colnum)+1;
// let json = JSON.stringify(data);
console.log('nColnum',nColnum);
cBlogTb.update({_id:ObjectID(blogid)},{$set:{colnum:nColnum}},(err,_res)=>{
// console.log('colnum',_res);
if (err) {
console.log("Error:" + err);
res.writeHead(300, {});
res.end('err');
}else{
res.writeHead(200, {});
res.end('ok');
console.log('ok','ok');
}
db.close();
});
});
}
}
//删除指定单篇文章
else if (req.url.indexOf('blog_delete')>=0){
var sBlogid = getReq['blogid'];
if(sBlogid){
let cBlogTb = db.collection('blog_tb');
cBlogTb.deleteOne({_id:ObjectID(sBlogid)},(err,_res)=>{
// console.log('colnum',_res);
if (err) {
console.log("Error:" + err);
res.writeHead(300, {});
res.end('err');
}else{
res.writeHead(200, {});
res.end('ok');
console.log('ok','deleteok');
}
db.close();
});
}else{
//404 没有这篇文章
}
}
//修改博客文章接口
else if (req.url.indexOf('blog_update')>=0) {
var sBlogid = getReq['blogid'];
if(sBlogid){
let jData = {};
jData['tittle'] = getReq['tittle'];
jData['brief'] = getReq['brief'];
jData['autor'] = getReq['autor'];
jData['detail'] = getReq['detail'];
jData['typeid'] = parseInt(getReq['typeid']);
jData['typename'] = getReq['typename'];
jData['updatetime'] = new Date();
// jData['readnum'] = 0;
// jData['comtnum'] = 0;
// jData['colnum'] = 0;
let cBlogTb = db.collection('blog_tb');
cBlogTb.update({_id:ObjectID(sBlogid)},{$set:jData},(err,_res)=>{
// console.log('colnum',_res);
if (err) {
console.log("Error:" + err);
res.writeHead(300, {});
res.end('err');
}else{
res.writeHead(200, {});
res.end('ok');
console.log('ok','ok');
}
db.close();
});
}
}
//删除指定留言内容
else if (req.url.indexOf('msg_delete')>=0){
var sMsgid = getReq['msgid'];
if(sMsgid){
let cMsgTb = db.collection('message_tb');
cMsgTb.deleteOne({_id:ObjectID(sMsgid)},(err,_res)=>{
// console.log('colnum',_res);
if (err) {
console.log("Error:" + err);
res.writeHead(300, {});
res.end('err');
}else{
res.writeHead(200, {});
res.end('ok');
console.log('ok','deleteok');
}
db.close();
});
}else{
//404 没有这篇文章
}
}
});
}
});
server.listen(port,hostname,()=>{
console.log(`Server running at http://${hostname}:${port}/`);
});
// var Mongo = require('./system/mongo/init.js');
// function dbGetVisit(callback){
// let MongoClient = require('mongodb').MongoClient;
// let dbUrl = 'mongodb://127.0.0.1:27017/blog';
// MongoClient.connect(url,(err,db)=>{
// if(err){
// console.log('err',err);
// return false;
// }
// let cAppInfo = db.collection('app_info');
// let json = JSON.stringify(cAppInfo.find().toArray());
// console.log('res-end',json);
// if (typeof callback === "function"){
// this.callback(json);
// }
// }
// }