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
|
const Router = require('koa-router');
const kb = require('./pieces/kb-util.js');
const data = require('./pieces/prepare-streams');
const products = require('./data/products.json');
const bench = require('./benchmark/find.js');
const macthStr = require('./benchmark/match-str.js');
// Prefix all routes with: /items
const router = new Router({
// prefix: '/items'
});
/* simple route example
let items = [
{ id: 100, iname: 'Quartz Analog Wrist Watch', price: 'US $4.99'},
{ id: 101, iname: 'Leather Peep Pump Heels', price: 'US $33.56'},
{ id: 102, iname: 'Apple iPod', price: 'US $219.99'},
{ id: 103, iname: 'Prince Phantom 97P Tennnis Racket', price: 'US $50.00'},
];
// Routes
router.get('/items', (ctx, next) => {
ctx.body = items;
next();
});
router.get('/items/:id', (ctx, next) => {
let getCurrentItem = items.filter(function(item) {
if (item.id == ctx.params.id) {
return true;
}
});
if (getCurrentItem.length) {
ctx.body = getCurrentItem[0];
} else {
ctx.response.status = 404;
ctx.body = 'Item Not Found';
}
next();
});
*/
// NOTE: set timeout per route (how-to)
// https://stackoverflow.com/questions/66634123/how-to-add-individual-timeout-value-per-specific-route-using-node-js
// Routes
router.get('/', (ctx, next) => {
ctx.body = {
success: true,
title: 'oseine',
description: 'oseine search engine is not elastic',
message: 'where are you now?'
}
next();
})
router.get('/search', (ctx, next) => {
ctx.body = [];
next();
});
router.get('/search/:title', (ctx, next) => {
// console.log(ctx);
let words = kb.keyboardize(kb.clean(ctx.params.title)).split(' ');
ctx.body = {
params: ctx.params,
results: words,
nxt: next
};
next();
});
/** other routes
* /////////////////////////////////////////////////////////////////////////////
* + /suggest/<some search string>
* + /prepare (products, keywords, linked-terms etc)
* + /stats
*/
// TEST routes
////////////////////////////////////////////////////////////////////////////////
router.get('/test', (ctx, next) => { // easy test route
// test anything ...
let result = { success: true, data: [1, 2, 3] };
ctx.body = result;
next();
});
router.get('/test/do-data', (ctx, next) => { // easy test route
// test anything ...
// data.create(
// 'products',
// 'https://storage.googleapis.com/pythia-files/uploads/json/emarket-products.json'
// );
data.load_products(
'https://storage.googleapis.com/pythia-files/uploads/json/emarket-products.json'
);
ctx.body = { success: true, operation: 'create new data' };
next();
});
router.get('/test/json', (ctx, next) => { // easy test route
// test anything ...
ctx.body = { t: + new Date(), p: products };
next();
});
router.get('/bench/fe', (ctx, next) => { // easy test route
// test anything ...
// ctx.body = bench.compare();
ctx.body = bench.compare();
next();
});
router.get('/bench/match', (ctx, next) => { // easy test route
// test anything ...
// ctx.body = bench.compare();
ctx.body = macthStr.run();
next();
});
module.exports = router;
|