-1
Hello fellow programmers!
I’ve been facing a problem for days and I’d like your help to solve it.
I am creating a project in Nodejs and Mongodb. It queries the database according to what the user types and brings the results in a list. For now I use pure Nodejs and bootstrap.
My next step is to create a simple pagination, because the list of items that comes out in the results is huge.
But since I’m still very much in the business, I’m looking for your help.
Below is my index.js
const express = require('express');
const router = express.Router();
const morgan = require("morgan");
const bodyParser = require('body-parser');
const path = require("path");
const mongoose = require('mongoose');
function simplify(text) {
const separators = /[\s,\.;:\(\)\-'\+]/g;
const diacritics = /[\u0300-\u036f]/g;
//capitalização e normalização
text = text.toUpperCase().normalize("NFD").replace(diacritics, "");
//separando e removendo repetidos
const arr = text.split(separators)
.filter((item, pos, self) => self.indexOf(item) == pos);
console.log(arr);
//removendo nulls, undefineds e strings vazias
return arr.filter(item => (item));
}
router.get('/search', function (req, res) {
res.render('search', {
title: 'Pesquisa'
});
});
router.get('/about', function (req, res) {
res.render('about', {
title: 'Sobre'
});
});
router.get('/', function (req, res, next) {
if (!req.query.q)
return res.render('index',
{
title: 'Site',
rb: [], query: ''
});
else {
const query = simplify(req.query.q);
const mongoClient = require("mongodb").MongoClient;
mongoClient.connect("mongodb://localhost:27017", {
useNewUrlParser: true,
useUnifiedTopology: true
})
.then(conn => conn.db("search-db"))
.then(
db => db.collection("tags")
.find({ tags: { $all: query } }, { title: { $all: query } })
.skip(10)
.limit(10)
)
.then(cursor => cursor.toArray())
.then(rb => {
return res.render('search',
{
title: 'Pesquisa',
rb, query: req.query.q
});
})
}
});
module.exports = router;