NodeJS - when using dynamic URL parameters - how can I return a 404 or redirect if no match is found with all URL parameters

Hey all,

So I’ve finished first pass of our dynamic directory using URL paths.

I can have up to 5 parameters in a URL path - currently if someone was to alter the URL it’ll just load the page and return the page structure but with no data as the SC won’t find a match.

I was wondering if anyone had ideas on how I could return a 404 page not found or even redirect the user?

The SC doesn’t return an error when no match found - I was thinking of simply throwing an error and using that but it doesn’t present any error.

You can throw a 404 response from the server action that is used in your SC.
In the server action, just add a condition step to check if there are 0 matches, and in that case, add a “response” step with status code 404.
With this in place, you can configure the dynamic event for 404 and redirect to some 404 page.

OR, you can also directly use the “redirect” step in the server action, when condition returns 0 rows. But, that redirect step is not recommended to be used in that manner (last I read about it here).

1 Like

Could someone help me to achieve this? I’m very newbie… :dizzy_face:

My case is more easy, I have a dynamic page that filters the database query with only 1 url parameter called urlitemid.

How do I check if there are 0 matches?

What I want to achieve is if there is no match instead of show the page with no content to the users redirect them to another page.

Thank you!

To check if a query is null you can use count:

1 Like

Thanks for your reply! I must be really dumb, cause I can’t figure out how to do that. :disappointed_relieved:

My action

The query:

Existing URL /test/1:
image

Non existing URL /test/12345:
image

What I’m trying to achieve is redirect to a 404 page the non existing URL’s. :cold_sweat:

I’m adding the example project in case someone wants to check it.

Thank you!

test404.zip (194.9 KB)

I think redirect only works if the API is used as the Server-Side data for the page. That maybe the only way to get a true 404.

Alternatively, you could set a value in the API for notfound and the use a condition if the notfound is present onsuccess of the SC action on the page to use the browser component (add it to your page if you haven’t already) to go to the 404 page:


(p.s. you don’t need to do a count on the query - it will return false in a condition server-side if there are no results)

or using the same notfound value, you could have two conditional regions on your page. One which shows the record if notfound is not present and one which shows a 404 style page if true (1). This is not a true 404 as it will return 200 but show a message to the user that the record wasn’t found.

1 Like

Thanks for your help @bpj , I tried what you said with no success but maybe is something wrong in my settings.

One quick question, this should return response 404 if no page found?

Cause never gives 404 no matter if founds a page or not:

Sorry for being that repetitive, but I find this step very important for my project. if I get it I will make a guide for wappler newbies, I think it is something very important. :slightly_smiling_face:

For the condition would you mind trying:
query.count()<1


I just tested getting a 404 error on one of my actions and this was working

1 Like

After this seen as this throws a 404 you can add a dynamic error event to the server action select the far right icon to use a in-line flow:

In the flow use a condition to check if the status of the action was a 404 then a run step with a browser goto

1 Like

Guys thanks for your time, really!

If I try that condition I always get the 404 response.

URL that exists:

URL that does not exist:

:thinking:

Does the query return something when the page is available?

My only other thought is the server action firing with incorrect params at times if it’s auto loading the new app connect is so fast that on a SPA if you come from a different page for example site.com/list then go to a url param like site.com/details/1 it tends to load twice the first one will load without params and there for the query would have been null then it runs the action again with params.

To check this in dev tools look in the Network Tab then XHR, then navigate to the page as normal and see if it fires the server action twice etc (This might be nothing but depends how your testing this)

1 Like

Thanks again @Sorry_Duh.

If i disable the condition it works just fine. Is this what you are asking?

On the other part all seems fine on XHR.

I attach the project in case you or anyone want to check it, is just 1 page with no content whatsoever and 1 api call.

Only pages that need to work and exist are:
http://localhost:3000/test/1
http://localhost:3000/test/2

Anything else should return 404 or redirect:
http://localhost:3000/test/123123 for example

If someone needs to be paid for help me just tell me, I want to figure out this really hard! :slight_smile:

Project:
test404.zip (196.8 KB)

test404.zip (257.8 KB)

Please note I am no expert but maybe this will help if this works I’ll go into more detail about the changes I made etc (I saw you was trying server side so I tried a server side solution using redirect)

1 Like

Thanks a lot! That is working perfectly!

I see you did a second api workflow, why? Also you placed itemurlglobal inside $_PARAM in this api. Then you used this new api in the server side data of the page.

Could you go in detail? From my point of view its very similar of what I have done with your help, why with this changes now it works? I would like to understand it better so I can learn from this.

Thank you! :clap:

I created a second action that used PARAM as GET didn’t pass the url variables to the server side data so the query always returned no records. You could do this in one action if you want to bind all your app side data with the server side bindings in code view when not on an element you see:

The second action was just because I find it easier to bind data from Server Connects over using the server side data bindings (maybe there is an easier way to bind them but I’ve not experimented much)

1 Like

An example of this is:

<p>Description: <%=_('query[0].description',locals)%></p>

Which will get the value from the Server Side Data, meaning you wouldn’t need the serverconnect1 on the page

1 Like