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

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.

Related

Dot in route parameter doesn't match route in sailsjs

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

how to get query parameter in action2 in sails

I dont know how to set up route according to REST for the search request url like this
localhost:1337/system?id=10
After that, how can we get parameter from the above request in action 2 like this
action2(inputs,exits)
I want to follow best practice for web api in sails. Please hint me to solve and recommend any handy documents.
Thanks in advance
For the Actions2 format, you'd use inputs.id, after properly defining the inputs, of course. For more information, consult the documentation on Actions and Controllers. This would need more finishing, but it gets your input and a function. You'd want to add in your exits, and some real logic, of course, and probably some decent descriptions.
module.exports = {
inputs: {
id: {
type: 'number',
required: true
}
},
exits: {
success: { ... },
error: { ... }
},
fn: async function (inputs, exits) {
var myId = inputs.id;
return exits.success(myId);
}
}

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)?

Sencha Touch: How to build a restful proxy url syntax?

Defined as a model and its associations, I wish the http calls to follow best practices of restful. For example, if I make the call
user.posts();
I wish to run a call http defined as
users/1/posts
If a call is then put on post with id 32 then the url of reference must be
users/1/posts/32
So I want to avoid using the filter property as is the default for a get
/posts.php?filter=[{"property":"user_id","value":1}]
This is because the api rest are already defined and I can not change them.
I would like to build a minimally invasive solution and reading on various forums the best way is to do an ovveride the method buildURL the proxy rest but was not able to retrieve all the data needed to build the final URL. Can anyone give me an example?
thanks
Try the following:
window.serverUrl = "192.168.1.XX"
var service = "login.php"
var dataTosend: {
username:"xx",
password: "yy"
}
var methode:"POST" / "GET"
this.service(service,methode,dataTosend,onSucessFunction,onFailureFunction);
onSucessFunction: function(res) {
alert("onSucessFunction"):
},
onFailureFunction: function(res) {
alert("onFailureFunction"):
},
service: function(svc, callingMethod, data, successFunc, failureFunc) {
Ext.Ajax.request({
scope: this,
useDefaultXhrHeader: false,
method: callingMethod,
url: window.serverUrl + svc,
params: data,
reader: {
type: 'json'
},
failure: failureFunc,
success: successFunc
});
I hope this will solve your problem...

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");