Hi Im new to node & sails and the same time doing some standards in my codes. In sails.js I currently have this, for example below
api/
controllers/
TestController.js
services/
TestServices.js
Mostly I can access the view with using only this URL format:
http://localhost/test/
http://localhost/test/<action>
If I add some prefixes to my filename, it will become like this:
api/
controllers/
PrtestController.js
services/
PrtestServices.js
URL by right should be accessible via:
http://localhost/prtest/
http://localhost/prtest/<action>
Questions:
1) if I add prefixes to all my controller & services filenames, is it possible to access the url by:
http://localhost/test/
http://localhost/test/<action>
without adding prefix?
I was thinking of configuring config/routes.js in order to achieve this by editing something like this:
'/test': {
controller: 'PrtestController',
action: '<action>'
},
'/test/*': {
controller: 'PrtestController'
}
Honestly, I haven't tried it yet (it's just an idea before I make major changes in my codes else I might messed it up)
2) Is it possible to have this in sails.js
PrtestController.js > prTestController.js
Thanks in advance!
EDITED QUESTION
Btw I'm using default sails config for controllers having values of:
blueprints: {
actions: true,
rest: true,
shortcuts: true,
prefix: ''
...
}
Example: (my routes.js will be looked like this)
'/test' : 'prTestController',
'/test/action2' : 'prTestController:action2',
'/test/action3' : 'prTestController:action3',
'/test/action4' : 'prTestController:action4',
'/test/action5' : 'prTestController:action5',
'/test/action6' : 'prTestController:action6',
'/test/action7' : 'prTestController:action7',
'/test/action8' : 'prTestController:action8',
'/test/action9' : 'prTestController:action9'
Is it possible for routes that if the url is having /test or /test/any_action will automatically used controller prTestController or prTestController:any_action, respectively?
For #2, yup that's what I mean.
Thanks much!
Yes, you have to edit the config/routes.js in order to do custom routing. If your controller is named PrtestController, then (if activated) the blueprints will automagically set up a route for you to host/prtest/. To override this, turn the blueprints off, and add some custom routes.
Sails docs on routes
If you are having difficulties understanding the magic in Sails, I suggest you to turn off all the blueprints, or at least play with the different settings. By doing this, you have to manually configure routes and actions. When you have an understanding of what the blueprint does and the reason for it, turn it back on if you want to use it.
Here is a video tutorial explaining blueprints and routes in more detail.
Not sure what you mean. Do you wonder if lowercase controllernames are allowed? If so, yes, that shouldn't be a problem.
Related
I am trying to clean my code and make it maintainable and testable so i am applying dependency injection
but i have faced alot of questions ..
First lets see my folder structure ..
User // Folder
index.js
User.js
DAL
services
etc(routes,controller)
in services i have to do DI so my services class should be like this
class UserServices {
constructor(DAL) {
this.DAL = DAL;
}
getAllUsers() {
return this.DAL.getAllUsers();
}
}
module.exports = UserServices;
and so on for the DAL it will recieve the Database in the constructor instead of DAL in the previous ex.
Now my question is ..
-Where do i give the User class the parameters if i am exporting the class not the instance ? (btw should i export the class or the instance ?)
Should i add the npm packages to the parameters too (nodemailer, jwt,etc)?
and if yes where should i give the parameters ?
i dont think in the controller because thats breaks the idea of the clean code..
Any helpful resource will be helpful alot..
Thanks for your time i appreciate it
I have API with Sails.js and I want to wrap all my routes in v1. Is it possible?
Here is what I tried, but it doesn't work.
routes.js
'use strict';
module.exports.routes = {
'/v1': { //
'get /cron': 'CronController.start' // THIS DOES NOT WORK
}, //
'get /cron': 'CronController.start' // this works
};
Based on my knowledge of Sails the only way to wrap all of your routes in /v1 is to first ensure the actions boolean in config/blueprints.js is set to true (it is by default), and then further down in that file set the prefix string to "/v1". Here is the documentation detailing this config.
Note that having the actions boolean set to true causes Sails to generate GET, POST, PUT, and DELETE routes for the action, make sure to use policies to ensure no unsafe logic is exposed in this way.
Unkown provider: $urlMatcherFactory
This is the error message that Angular throws when I try to inject $urlMatcherFactory in the app config.
I have included the ui-router JS file and have been using it for last few months.
I need to do something like this:
var dateMatcher = $urlMatcherFactory.compile("/day/{start:date}");
$stateProvider.state('calendar.day', {
url: dateMatcher
});
as this example shows.
Normally code like this won't identify $urlMatcherFactory. So I tried injecting it as a dependency on the config but then I get this error.
In this Q&A Matching url with array list of words in AngularJS ui-router you can see how to use the $urlMatcherFactory. Also a link to working plunker .
In that example, the $urlMatcherFactory is used in the .run():
.run(['$urlMatcherFactory',
function($urlMatcherFactory) {
var sourceList= ['John', 'Paul', 'George', 'Ringo']
var names = sourceList.join('|');
var urlMatcher = $urlMatcherFactory.compile("/{source:(?:" + names + ")}/:id");
$stateProviderRef
.state('names', {
url: urlMatcher,
templateUrl: 'tpl.root.html',
controller: 'MyCtrl'
});
}
]);
And that would also mean, that if you are about to use it in a config phase, you should ask for
$urlMatcherFactoryProvider (see the Provider at the end)
BUT: using providers in a config phase means - we can configure them. I mean configure the provider itself. To be later evaluable (already configured) in run phase
Configuring Providers
You may be wondering why anyone would bother to set up a full-fledged provider with the provide method if factory, value, etc. are so much easier. The answer is that providers allow a lot of configuration. We've already mentioned that when you create a service via the provider (or any of the shortcuts Angular gives you), you create a new provider that defines how that service is constructed. What I didn't mention is that these providers can be injected into config sections of your application so you can interact with them!
First, Angular runs your application in two-phases--the config and run phases. The config phase, as we've seen, is where you can set up any providers as necessary. This is also where directives, controllers, filters, and the like get set up. The run phase, as you might guess, is where Angular actually compiles your DOM and starts up your app.
I'm creating my first sizable angular project, so I've opted to break out my angular application code into directories like so:
app/
calendar/
calendar.js
contacts/
contacts.js
companies/
companies.js
...
app.js
Each of these directories will include a handful of files for views (.html), directives, services, etc.
In my app.js file, I want to setup default routes that will be used maybe 80% of the time, like this:
$routeProvider
.when('/:page', {
templateUrl: function($routeParams) {
return 'app/' + $routeParams.page + '/index.html';
},
controller: 'PageController'
})
However, I want the ability to "override" that route from within my modules so I can do things like swap out the controller, do dependency injection stuff, and so on. I would like that sort of logic contained within the module itself to keep things...well...modular, so I would rather not define each module's routing logic within app.js. So for example, in my app/calendar/calendar.js file I want to say:
$routeProvider
.when('/calendar', {
templateUrl: 'app/calendar/index.html',
controller: 'CalendarIndexController',
resolve: { ... }
})
The problem is that the definition in app.js is matched against the location first, so this calendar route is never used.
To achieve this, right now I'm just including an extra file after all of my module javascript files that sets up my fallback routes last, but this seems a little clunky. Is there anyway to define the routes within app.js so that they are override-able?
You might be able to adapt this technique for dynamically loading controllers described by Dan Wahlin here http://weblogs.asp.net/dwahlin/dynamically-loading-controllers-and-views-with-angularjs-and-requirejs combined with your default routes where the parameter controls which view to load. You could pass the same page parameter to the resolve function to control which combination of view and controller to serve up?
I'm using RequireJS to load my JavaScript modules for a BackboneJS web app, but I'm running into a style issue at the moment.
I have two classes that share a LOT of code, but they both subclass different classes.
TemplateLessonView LessonView
| |
| |
EditorApp <-- these share code --> MainApp
Now to keep things DRY I'd like MainApp to extend EditorApp, but then EditorApp's dependencies are loaded and EditorApp calls some of it's base class' functions by means of this format: this.constructor.__super__.initialize.apply(this);
A solution would be to change EditorApp's dependencies when the module is loaded from MainApp's module (so I would require LessonView but pass it to EditorApp as TemplateLessonView.
Does anyone know of a way to do this, or has another suggestion to accomplish what I'm trying here?
On way to solve this is to create a module that you will import in both class definition...
something like that :
(...require stuff ...)
return {
myfct1 : function(){
},
myfct2 : function(){
}
}
and then you can import the module where you need it, and then call your common code like a library...