index.js
1.5 KB
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
var express = require('express');
var createError = require('http-errors');
var router = express.Router();
const post = require('../../../model/video');
const tags = require('../../../model/tagList');
router.get('/list', (req, res, next) => {
let { tags, skip } = req.query;
let joinedTag;
console.log(tags);
if (tags) joinedTag = tags.join('|');
else {
joinedTag = '';
}
let regexsearch = { tag: { $regex: joinedTag, $options: 'si' } };
skip = parseInt(skip);
post
.find(regexsearch)
.sort({ _id: -1 })
.limit(10)
.skip(skip)
.then((rs) => {
res.send({ success: true, d: rs });
})
.catch((e) => {
console.log(e);
res.send({ success: false, msg: e.message });
});
});
router.get('/tag', (req, res, next) => {
tags
.find()
.then((rs) => {
var temp = [];
rs.forEach((element) => {
element.tag.forEach((element) => {
temp.push(element);
});
});
var uniqArray = Array.from(new Set(temp));
res.send({ success: true, d: uniqArray });
})
.catch((e) => {
console.log(e);
res.send({ success: false, msg: e.message });
});
});
router.delete('/:_id', (req, res, next) => {
const _id = req.params._id;
post
.findOne({ _id })
.then((r) => {
console.log(r);
return post.deleteOne({ _id });
})
.then((r) => {
res.send({ success: true, d: r });
})
.catch((e) => {
console.log(e);
res.send({ success: false, msg: e.message });
});
});
router.all('*', function (req, res, next) {
next(new Error('Wrong Url!'));
});
module.exports = router;