-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
121 lines (106 loc) · 3.77 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
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
const express = require('express');
const sqlite = require('sqlite');
const Promise = require('bluebird');
const mergeTwoArrayContent = require('./utility/helper');
const app = express();
const dbPromise = sqlite.open('./db/database.db', { Promise });
app.get('/api/eTob/:eng', async (req, res, next) => {
try {
const wordToSearch = req.params.eng.toUpperCase();
console.log('Searching for => ', wordToSearch);
const db = await dbPromise;
// console.log('Connected to the dictionary database.');
const english = await db.all(
`SELECT * FROM eng WHERE word LIKE "${wordToSearch}%" LIMIT 5`
);
// const english have something like => [{serial: 48141, word: 'DOG'}, {}, {}]
// Iterate through english array and create query
const queries = [];
english.forEach(singleItem => {
queries.push(`SELECT * FROM other WHERE serial= ${singleItem.serial}`);
});
// search singleItem serial in other table
const other = await Promise.all(
queries.map(singleQuery => db.get(singleQuery))
);
res.json(mergeTwoArrayContent(english, other));
} catch (err) {
next(err);
}
});
app.get('/api/bToe/:bangla', async (req, res, next) => {
try {
const wordToSearch = req.params.bangla.toUpperCase();
console.log('Searching for => ', wordToSearch);
const db = await dbPromise;
// console.log('Connected to the dictionary database.');
const other = await db.all(
`SELECT * FROM other WHERE word LIKE "${wordToSearch}%" LIMIT 5`
);
const queries = [];
other.forEach(singleItem => {
queries.push(`SELECT * FROM eng WHERE serial= ${singleItem.serial}`);
});
const english = await Promise.all(
queries.map(singleQuery => db.get(singleQuery))
);
res.json(mergeTwoArrayContent(english, other));
} catch (err) {
next(err);
}
});
app.get('/api/exact/eTob/:eng', async (req, res, next) => {
try {
const wordToSearch = req.params.eng.toUpperCase();
console.log('Searching Exact for => ', wordToSearch);
const db = await dbPromise;
// console.log('Connected to the dictionary database.');
const english = await db.all(
`SELECT * FROM eng WHERE word = "${wordToSearch}"`
);
// const english have something like => [{serial: 48141, word: 'DOG'}, {}, {}]
// Iterate through english array and create query
const queries = [];
english.forEach(singleItem => {
queries.push(`SELECT * FROM other WHERE serial= ${singleItem.serial}`);
});
// search singleItem serial in other table
const other = await Promise.all(
queries.map(singleQuery => db.get(singleQuery))
);
res.json(mergeTwoArrayContent(english, other));
} catch (err) {
next(err);
}
});
app.get('/api/exact/bToe/:bangla', async (req, res, next) => {
try {
const wordToSearch = req.params.bangla.toUpperCase();
console.log('Searching Exact for => ', wordToSearch);
const db = await dbPromise;
// console.log('Connected to the dictionary database.');
const other = await db.all(
`SELECT * FROM other WHERE word = "${wordToSearch}"`
);
const queries = [];
other.forEach(singleItem => {
queries.push(`SELECT * FROM eng WHERE serial= ${singleItem.serial}`);
});
const english = await Promise.all(
queries.map(singleQuery => db.get(singleQuery))
);
res.json(mergeTwoArrayContent(english, other));
} catch (err) {
next(err);
}
});
// Server static assets if in production
if (process.env.NODE_ENV === 'production') {
// Set static folder
app.use(express.static('client/build'));
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'));
});
}
const port = process.env.PORT || 5000;
app.listen(port, () => console.log(`Server running on port ${port}`));