separate search route

This commit is contained in:
json 2021-09-02 20:58:38 +01:00
parent 74c202d5f0
commit c0e20b5817
2 changed files with 50 additions and 0 deletions

View File

@ -2,6 +2,7 @@ const homeRoute = require('./home');
const overridingRoutes = require('./overides'); const overridingRoutes = require('./overides');
const preferenceRoutes = require('./preferences'); const preferenceRoutes = require('./preferences');
const saveRoutes = require('./save'); const saveRoutes = require('./save');
const searchRoute = require('./search');
const staticRoutes = require('./static'); const staticRoutes = require('./static');
const subredditRoutes = require('./subreddit'); const subredditRoutes = require('./subreddit');
const subscriptionRoutes = require('./subscription'); const subscriptionRoutes = require('./subscription');
@ -16,6 +17,7 @@ allRoutes.use(subredditRoutes);
allRoutes.use(userRoutes); allRoutes.use(userRoutes);
allRoutes.use(subscriptionRoutes); allRoutes.use(subscriptionRoutes);
allRoutes.use(saveRoutes); allRoutes.use(saveRoutes);
allRoutes.use(searchRoute);
allRoutes.use(homeRoute); allRoutes.use(homeRoute);
module.exports = allRoutes; module.exports = allRoutes;

48
routes/search.js Normal file
View File

@ -0,0 +1,48 @@
const searchRoute = require('express').Router();
searchRoute.get('/search', (req, res, next) => {
let q = req.query.q;
if (typeof q === 'undefined') {
return res.render('search', {
json: { posts: [] },
no_query: true,
q: '',
restrict_sr: undefined,
nsfw: undefined,
subreddit: 'all',
sortby: undefined,
past: undefined,
user_preferences: req.cookies,
});
}
let restrict_sr = req.query.restrict_sr;
let nsfw = req.query.nsfw;
let sortby = req.query.sort;
let past = req.query.t;
let after = req.query.after;
let before = req.query.before;
if (!after) {
after = '';
}
if (!before) {
before = '';
}
if (restrict_sr !== 'on') {
restrict_sr = 'off';
}
if (nsfw !== 'on') {
nsfw = 'off';
}
let d = `&after=${after}`;
if (before) {
d = `&before=${before}`;
}
return res.redirect(
`/r/all/search?q=${q}&restrict_sr=${restrict_sr}&nsfw=${nsfw}&sort=${sortby}&t=${past}${d}`
);
});
module.exports = searchRoute;