-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
65 lines (55 loc) · 1.92 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
const express = require('express');
const sqlite3 = require('sqlite3').verbose();
const path = require('path');
const app = express();
const PORT = process.env.PORT || 3000;
// Middleware to parse JSON request bodies
app.use(express.json());
// Static files from specific folders
app.use('/scripts', express.static(path.join(__dirname, 'scripts')));
app.use('/styles', express.static(path.join(__dirname, 'styles')));
app.use('/img', express.static(path.join(__dirname, 'img')));
app.use('/data', express.static(path.join(__dirname, 'data')));
// HTML files in the "pages" folder
app.use('/', express.static(path.join(__dirname, 'pages')));
// Open the SQLite database from the correct path
const db = new sqlite3.Database(path.join(__dirname, 'data/sql/leishmania.db'), (err) => {
if (err) {
console.error('Error opening database', err.message);
} else {
console.log('Connected to the SQLite database.');
}
});
app.get('/search', (req, res) => {
const query = req.query.q || '';
const page = parseInt(req.query.page) || 1;
const limit = parseInt(req.query.limit) || 50; // Default limit of 50 rows per page
const offset = (page - 1) * limit;
const sql = `
SELECT * FROM infantum
WHERE Gene_ID LIKE ?
OR Name LIKE ?
OR Other_names LIKE ?
OR Wikidata LIKE ?
OR Mendeley LIKE ?
OR UniProt LIKE ?
OR LmjF_ortholog LIKE ?
OR LmjFC_ortholog LIKE ?
OR LdHU3_ortholog LIKE ?
OR LBRM2904_ortholog LIKE ?
LIMIT ? OFFSET ?;
`;
const params = Array(10).fill(`%${query}%`);
params.push(limit, offset);
db.all(sql, params, (err, rows) => {
if (err) {
res.status(500).send('Error querying database');
} else {
res.json(rows);
}
});
});
// Start the server
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});