Alter Query Strings Server Side - node.js

I have an Express application that gets search parameters and page numbers via query strings.
var name = req.query.name;
var state = req.query.state;
var category = req.query.category;
var pageNum = req.query.pagenum;
What I want is if the search query has a page number and yields no results, to do a search without the page number and pass that back to the client. This works fine, and the front-end is receiving the data correctly.
The only thing is that I want the URL to reflect the changed page number search, so rather than being website.com/search?page=4&state=AL I'd like it to have the page number stripped or changed to 1. I can do this via Javascript on the front-end, but I'd rather have the url changed on the server side (it makes more sense to me at least, rather than having to change every template to do that).
EDIT:
Looks like the only way of doing this is to redirect to the URL without the page query string parameter. It seems this will help me out in getting this done.

I don't quite know your environment, but generally URLs cannot be "changed" on server-side, because they are displayed in the browser on the client-side. The user sees the last whole page request's url. You can only force an URL update (actually a page reload with different URL) from server-side with a HTTP redirect header.
You could check if there are any results for the query with page number, and if not, simply send a redirect to the URL without the page number, so the user sees that extended result with its proper (page-less) URL.
Hope it helps something.

Related

Caching api response using react-query when changing routes but be able to receive data when reloading the page

I have prepared a simple demo with react-router-dom 6 and react query.
I have a couple of routes and a fetch call that takes place on the first route (Home).
What I want to achieve is navigating to the About page or any other page and do not perform another request for a certain time period (maybe never again) but if I refresh the page I want to be able to re trigger the request to get the data.
I have tried using staleTime when but if I refresh the page I get no results, just a blank page. refreshInterval works on refresh but does not keep the data when I change routes.
I have also tried this pattern in this article but still I don't get the job done.
Probably it may be that something I don't understand but the question is how do I avoid making the same request over and over again, perfrm it only once but still being able to get the data if I refresh the page when navigating between different routes.
Demo
The solution to the problem came from one of the maintainers on the official github repo eventually and is related to adding placeholderData as an empty array instead of initialData and setting staleTime to Infinity in my case as I only want to perform the request once.
Setting placeholderData gives you the opportunity to show some dummy data normally until you fetch the real but in my case it seems to do the job. More to read regarding this at this source
const { isFetching, data: catImages } = useQuery({
queryKey: ["catImages"],
queryFn: getCatImages,
placeholderData: [],
staleTime: Infinity
});

Angular/NodeJS - Mongoose Pagination

This is more a design question than a lot of code.
I have my angular client query a mongodb collection via http and nodejs backend.
I want to paginate the result, so what I do is on the angular side, keep track of the page I'm on and the # of results I would like on each page, and I pass that on to my backend via an http call.
Then my backend has code that looks something like this:
schema.find({name: query, _userId: req.body._userId}).sort({ name: 'asc' }).limit(req.body.num_results).skip(req.body.page * req.body.num_results).then(response => {
Now I'd like to put "<" and ">" arrows on my client's html where every time left or right is clicked it traverses pages (via a new http call), but then make it so that "<" is disabled on page 0, and ">" is disabled on the last page of results. I've figured out how to disable the "<" - simply make the button go away when you're on page 0. But having trouble figuring out the best way to discover that I'm on the last page. One method could be for my backend to return the total number of results and then my front-end tracks where I am relative to the total #, but that involved another db call, move variables passed over http, more management on the front-end. Any ideas on how to implement a 'cleaner' solution?
Thank you.
You do not need to make any additional api call. You can return any additional information in the form of Http header when making api call. so, in this case return X-TOTAL-COUNT

Parse-Server query fed directly from URL query string

I'd like to know if this is even possible. And if it is possible, what the security ramifications would be.
I want to use Javascript to build a dynamic URL to query a Parse-Server database.
It appears that it might be possible based on an earlier Stackoverflow question here and a Node.js doc here
Here's how I envision it working....
So, a user would be sent (via email/Twitter/etc) a link which was created by above method. Once the user clicked on that URL link, the following would happen automatically:
Step #1: User's system would automatically submit a parse-server query.
Step #2: On success, the user's browser would download a web page which displayed the query results.
step one create the pointer value ie the query pseudo-semantic
step 2 insert the pointer value to a text-type field in parse cls=clazz
step 2b mailgun a msg containing a link like :
express.domain.com/#!clazz/:yALNMbHWoy - where 'yA...oy' is oid of pointer row in parse/clazz table
Note that the link is an abstraction only. Its a uri first to express/route && function that will simply get a row from parse.clazz. That row contains the semantic for making a parse query to get back the full DB compliment to pass along to the node template setting up the html ...
in your node/router GET/clazz/:oid will lookup that Parse row in parse/clazz, using the pointer/text value to format a 2nd, Parse query. The query response from the 2nd parse.qry is your real meat ... This response can be used by the express template formatting the html response to orig request on "express.domain.com".
where u ask "download web page" .. that is simply node's RESPONSE to a GET on a route like "GET/clazz".

How to get previous URL in JSF using FacesContext?

I need to get the redirected URL or id in JSF using FacesContext. For current URL, I'm using.
String currentPage = FacesContext.getCurrentInstance().getViewRoot().getViewId();
Closest you can get is the referer header via ExternalContext#getRequestHeaderMap():
String referrer = externalContext.getRequestHeaderMap().get("referer");
// ...
You should only keep in mind that this is a client-controlled value and can thus be fully spoofed from client side on (i.e. the enduser can easily edit or even remove it).
Even then, there are cases where the client application won't send it along. For an overview, see among others this question: In what cases will HTTP_REFERER be empty.
Depending on the functional requirement, you'd better manually pass it along as request parameter, or store it in view or session scope.

Facebook appends query string to liked pages

When you like a page on Facebook the link to the page now appears on the users news feed with a query string appended to the end e.g
?fb_action_ids=102187656601495&fb_action_types=og.likes&fb_source=aggregation&fb_aggregation_id=46965925417
This 404s on every single page on my site.
I can change the facebook 'data-href' parameter on all my pages so the link will work by appending my own query string (?opendocument - the site is in lotus domino)
This works but means losing all the likes already in place on the page and effectively returning to zero likes for all the pages.
Is there anyway I can stop facebook appending the query string? If not can I update the 'data-href' paramater on my pages but still maintain the like count already in place.
Many Thanks in advance for any guidance here
Pete

Resources