-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
135 lines (118 loc) · 4.1 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
import dotenv from 'dotenv';
import express from 'express';
import morgan from 'morgan';
import cors from 'cors';
import quote from './models/mongodb.js';
dotenv.config();
const app = express();
const PORT = process.env.PORT;
app.use(cors());
app.use(express.json());
app.use(express.static('dist'));
morgan.token('body', (req) => JSON.stringify(req.body));
morgan.token('bodyLength', (req) => (JSON.stringify(req.body)).length);
app.use(morgan(':method :url status :status - :response-time ms content: :body :bodyLength Length :res[header]'));
app.get('/api', (req, res) => {
const page = parseInt(req.query.page) || 1;
const limit = 10;
const skip = (page - 1) * limit;
const strict = req.query.strict === 'true';
const searchTerm = strict ? `\\b${req.query.searchTerm}\\b` : req.query.searchTerm || '';
console.log(strict, searchTerm);
quote.aggregate([
{
$search: {
index: "default", // Specify your search index
phrase: {
query: searchTerm, // The search term (e.g., 'brain chip')
path: "text" // The field you're searching in
}
}
},
{
$group: {
_id: "$video_id",
video_id: { $first: "$video_id" },
quotes: {
$push: {
text: "$text",
line_number: "$line_number",
timestamp_start: "$timestamp_start",
title: "$title"
}
}
}
},
{ $skip: skip },
{ $limit: limit }
])
.then(result => {
res.json({ data: result });
})
.catch(error => res.status(500).send({ error: 'Something went wrong' }));
});
// app.get('/stats', async (req, res) => {
// try {
// const stats = await quote.aggregate([
// {
// $group: {
// _id: "$channel_source",
// distinctVideos: { $addToSet: "$video_id" },
// }
// },
// {
// $project: {
// channel_source: "$_id",
// videoCount: { $size: "$distinctVideos" },
// videos: "$distinctVideos"
// }
// },
// {
// $sort: { videoCount: -1 }
// }
// ]);
// res.json({ data: stats });
// } catch (error) {
// console.error('Error fetching stats:', error);
// res.status(500).json({ error: 'Failed to fetch stats' });
// }
// });
// // app.get('/stats', async (req, res) => {
// // try {
// // // Fetch distinct video IDs and their channel_source counts
// // const channelSourceCounts = await quote.aggregate([
// // {
// // $group: {
// // _id: "$channel_source", // Group by channel_source
// // total: { $sum: 1 } // Count occurrences
// // }
// // }
// // ]);
// // // Format the response for clarity
// // const result = channelSourceCounts.map(item => ({
// // channel_source: item._id,
// // count: item.total
// // }));
// // res.json({ data: result });
// // } catch (error) {
// // res.status(500).send({ error: 'Failed to fetch stats' });
// // }
// // });
app.get('/info', (req, res) => {
Person.find({}).then(person => {
res.send(`Phonebook has info for ${person.length} persons <br> Info is correct as of ${new Date().toISOString()}`);
});
});
const errorHandler = (error, req, res, next) => {
console.error(error.message);
if (error.name === 'CastError') {
return res.status(400).send({ error: 'malformatted id' });
} else if (error.name === 'ValidationError') {
return res.status(400).json({ error: error.message });
}
next(error);
};
app.use(errorHandler);
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});