Autocomplete

This commit is contained in:
2020-07-21 19:40:27 +02:00
parent 9f4f5347d4
commit 0bc5022fb2
2 changed files with 40 additions and 1 deletions

View File

@@ -6,6 +6,7 @@ import routeDish from "../routes/routeDish.js";
import routeCity from "../routes/routeCity.js";
import routeRestaurant from "../routes/routeRestaurant.js";
import routeUser from "../routes/routeUser.js";
import routeSearch from "../routes/routeSearch.js";
import routeImg from "../routes/routeImg.js";
import cookieParser from "cookie-parser";
@@ -15,9 +16,9 @@ export default ({ app, secret }) => {
max: 100, //requests from a single IP for a time window
});
app.use(cors());
app.use(helmet());
app.use(limiter);
app.use(cors());
app.use(bodyParser.json({ limit: "100kb" })); // limit JSON body payload size
app.use(bodyParser.urlencoded({ extended: true }));
app.use(function (err, req, res, next) {
@@ -29,6 +30,7 @@ export default ({ app, secret }) => {
app.use("/restaurant", routeRestaurant);
app.use("/img", routeImg);
app.use("/user", routeUser);
app.use("/search", routeSearch);
return app;
};

37
routes/routeSearch.js Normal file
View File

@@ -0,0 +1,37 @@
import express from "express";
import * as services from "../services/services.js";
import Restaurant from "../models/restaurant.js";
import sanitizer from "string-sanitizer";
import restaurant from "../models/restaurant.js";
var router = express.Router();
// AUTOCOMPLETE
router.get("/autocomplete/", (req, res) => {
var query = sanitizer.sanitize.keepUnicode(decodeURI(req.query.string));
const regex = new RegExp(query, "i");
let cities = new Set();
let restaurants = new Set();
Restaurant.find(
{ $or: [{ city: { $regex: regex } }, { name: { $regex: regex } }] },
"name city",
(err, doc) => {
if (err) {
res.send(404);
} else {
doc.forEach((value) => {
cities.add(value.city);
restaurants.add(value.name);
});
res.send({
cities: Array.from(cities),
restaurants: Array.from(restaurants),
});
}
}
).limit(10);
});
export default router;