Dot in route parameter doesn't match route in sailsjs - sails.js

One of my route parameters has a dot in the route parameter. The route is /len/:abc. Here abc is going to be -2.2,3.2,5.4. When I call the requeust /test/len/-2.2,3.2,5.4 I get a 404 error. When I remove the dots and call the request /test/len/-22,32,54 this works. Can anyone please help.
Below is my routes.js code -
'get /test/len/:abc': {
controller: 'TestController',
action: 'len',
}
Sails version 1.5.1
Node version 14.21.2

Try adding the skipAssets: false option.
'get /test/len/:abc': {
controller: 'TestController',
action: 'len',
skipAssets: false // include dots in the variable
}
See: https://sailsjs.com/documentation/concepts/routes/custom-routes#?route-target-options

Related

Axios - Sending a GET with & sign

I have a GET call (/getTag) that has a variable 'name'.
One of my users created on with a & sign. And unfortunately the GET call is now failing because it looks like this.
/getTag?name=IS&me-1234
Unfortunately my server interprets it like this because of the & sign:
{ id: 'IS', 'me-1234': '' }
Anyone experienced this before and have a way to solve it?
You should use encodeURIComponent on a variable name before passing it to axios.
You can use params key in axios
axios.get('/getTag', {params: {name: 'Is&me123'}})
You should request like:
axios.get('https://your-site.com/getTag', { params: { name: 'IS&me-1234' } });
instead of:
axios.get('https://your-site.com/getTag?name=IS&me-1234');

Sails.js: How to automatically route to standalone actions with wildcard?

Let's say that I have a few standalone routes in my Sails.js app (v1.0.2):
'user/login/',
'user/logout',
'user/reset-password'
...
Now, my current routes looks like that:
'GET /api/user/login': {
action: 'user/login',
},
'GET /api/user/logout': {
action: 'user/logout',
},
'GET /api/user/reset-password': {
action: 'user/reset-password',
},
Is there a way to get the same results with less code? something like:
'GET /api/user/*': {
action: 'user/*',
},
or:
'GET /api/user/:actionName': {
action: 'user/:actionName',
},
When using a route with a wildcard, such as '/*', be aware that this will also match requests to static assets (i.e. /js/dependencies/sails.io.js) and override them.
https://sailsjs.com/documentation/concepts/routes/custom-routes#?wildcards-and-dynamic-parameters
Or you can try using pattern variables to have less code.
May be you can activated
Automatically expose implicit routes for every action in your app?
in config/blueprints.js file.
Like this all your routes will be exposed and you won't need to specify each route and action.
But it is not a good solution for security reason.

Implementing Pass-Through with Grails Filters

Please note: Although I'm using the Grails Webflow plugin here, I am pretty sure this is just a generic question that any battle-weary Grails veteran could answer.
Grails 2.4.4 here. I have a need for a Grails Filter that inspects all traffic coming into a particular controller. If the app is in a particular state, I need to redirect traffic to another controller/action. Else, I need the filter to act as a no-op/pass-through filter (meaning it does nothing; just allows the request to pass through it and on to its intended destination):
package filters
class MyAppFilters {
def filters = {
fizzFilter(controller: 'fizz', action: '*') {
before = {
if(AppStateHolder.checkState() == AppState.Blue) {
redirect(controller: 'auth', action: 'unauthorized')
return false
} else {
// Allow request to continue on to its intended controller/action target ('no-op'/pass-through)
???
}
}
}
}
}
I have everything working perfectly for when the app state is AppState.Blue (so, the if-condition). The problem is that when app state isn't "blue", and that else executes, I keep getting infinite redirect errors. I believe Grails Webflow is complicating things, but I can't really fix anything with how it has been implemented.
I've also tried returning true/false from inside the else like so:
} else {
// Allow request to continue on to its intended controller/action target ('no-op'/pass-through)
return false // Also tried 'true'
}
But this produces the same infinite redirect errors. It looks like I need to do some kind of redirect/render/etc. inside this else or webflow will cause problems.
So I'm looking for a way to tell Grails to redirect to whatever was the controller/action off the request. Something like:
} else {
// Allow request to continue on to its intended controller/action target ('no-op'/pass-through)
redirect(controller: req.controller, action: req.action)
return false
}
Is this possible, if so, how (specifically)?

Why the port number is cut when angular app call rest service

I have rest service:
http://localhost:3000/api/brands
When I test it from web browser it works well.
I use it in angular service:
var motoAdsServices = angular.module('motoAdsServices', ['ngResource']);
motoAdsServices.factory('Brand', ['$resource', function($resource) {
return $resource('http://localhost:3000/api/:id', {}, {
query: {
method: 'GET',
params: {
id: 'brands'
},
isArray: true
}
});
}]);
When it is call I have error because in reqest URL I don't have port number:
Request URL: http://localhost/api/brands
Why the port numer is is cut? Angular cut it?
In Angular doc is written:
A parametrized URL template with parameters prefixed by : as in /user/:username. If you are using a URL with a port number (e.g. http://example.com:8080/api), it will be respected.
UPDATE
I use angular version 1.0.8 (thank #KayakDave for your attention) but Angular doc applies to version 1.2.
It has a colon and therefore gets stripped, kind of like :id. If you escape it, you should be ok. Try http://localhost\:3000/api/:id instead. You may run into this again in routes or other places.
There is an issue regarding this behavior in case something changes.
Updated: http://localhost\\:3000/api/:id
Because angular intercept the url and consider the :any as a parameter that you should pass to it.
An easy way to hack this is to put \:3000 in your url.
motoAdsServices.factory('Brand', ['$resource', function($resource) {
return $resource('http://localhost:3000\:3000/api/:id', {}, {
query: {
method: 'GET',
params: {
id: 'brands'
},
isArray: true
}
});
}]);
Quick fix for the resource plugin issue:
var fixedTargetUrl = TARGET_URL.replace(/(:\d{4})/, "\\$1");

Getting a 204 No Content response when I use restSetResponse in Coldfusion 10 REST

I am trying to use restSetResponse to set my own status and content as described in the docs here:
http://help.adobe.com/en_US/ColdFusion/10.0/Developing/WSe61e35da8d318518-5719eac51353e6bb244-7fec.html
Here is my REST method:
remote any function test() httpmethod="get" {
var response = {
status: 400,
content: 'something is wrong'
};
restSetResponse(response);
}
Any help would be greatly appreciated. Thanks!
In the link you provided, note the first numbered bullet under the "Send custom success responses using restSetResponse" heading. If you define the CFC as REST service, you need to ensure the returnType is set to "void". In your example, you have your returnType set to "any".