-
Notifications
You must be signed in to change notification settings - Fork 3
/
search.js
31 lines (27 loc) · 1.08 KB
/
search.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
import { config } from 'dotenv';
import { Router } from 'express';
import algoliasearch from 'algoliasearch/lite.js';
config();
const router = Router();
router.get('/:lang/search', (req, res, next) => {
if(process.env.algoliaApp === undefined || process.env.algoliaKey === undefined || process.env.indexName === undefined) {
res.send('Algolia search not enabled, please follow the instructions at https://github.com/kontent-ai/sample-app-express#algolia-search-integration');
}
const client = algoliasearch(process.env.algoliaApp, process.env.algoliaKey);
const index = client.initIndex(process.env.indexName);
const term = req.query.searchtext;
index.search(term).then(({ hits }) => {
res.render('search', {
'hits': hits,
'term': term
}, (err2, html) => {
if (err2) {
next(err2);
} else {
res.send(html);
res.end();
}
});
});
});
export default router;