-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
68 lines (55 loc) · 1.73 KB
/
server.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
const express = require('express');
const mongoose = require('mongoose');
const cors = require('cors');
const app = express();
app.use(cors());
// Connexion à MongoDB
mongoose.connect('mongodb://localhost/dblp', {
useNewUrlParser: true,
useUnifiedTopology: true,
});
// Création d'un schéma pour la collection "dblp"
const authorsSchema = new mongoose.Schema({
_id: String,
type: String,
title: String,
pages: {
start: Number,
end: Number,
},
year: Number,
booktitle: String,
url: String,
authors: [String],
});
const Authors = mongoose.model('Authors', authorsSchema, 'dblp');
// Endpoint API pour récupérer les données avec pagination
app.get('/api/authors', async (req, res) => {
const { page = 1, limit = 10, search = '', sort = 'title', order = 'asc' } = req.query;
console.log('Query parameters:', req.query); // Log parameters
try {
const query = {
$or: [
{ title: { $regex: search, $options: 'i' } },
{ authors: { $regex: search, $options: 'i' } },
],
};
console.log('MongoDB query:', query); // Log MongoDB query
const totalCount = await Authors.countDocuments(query);
const totalPages = Math.ceil(totalCount / limit);
const authors = await Authors.find(query)
.sort({ [sort]: order === 'desc' ? -1 : 1 })
.skip((page - 1) * limit)
.limit(parseInt(limit));
console.log('Results:', authors); // Log results
res.json({ authors, totalPages });
} catch (error) {
console.error('Error fetching data:', error);
res.status(500).json({ error: 'Internal server error' });
}
});
// Démarrage du serveur
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});