I'm new to Express and programming in general.
If you follow this book you learn to create a single Router() which maps all the routes to all the controllers. You end up having:
controllers
users.js
(other controllers....)
routes
routes.js
This router is then introduced to your app via a single call to app.use(router).
However, if you generate your app via express-generator, it will generate a sample structure with multiple Router()s, which are then mounted to your app via multiple app.use() calls. For example:
app.use('/users', users) // mounts a Router()
app.use('/posts', posts) // mounts another Router()
It seems that Express prefers to have a seperate Router mounted for each controller. Have I got it right?
In the express-generator example, callbacks are hard-wired into routes. Let's say you want to introduce controllers into your code.
You would end up having something like this?
controllers
users
routes.js
callbacks.js
[other controller]
[its Router()].js
[its callbacks].js
My questions is, which is the better way to go?
Is there some architectural concept I misunderstand? (Maybe I should be hard-wiring callbacks into routes when I'm usign multiple Routers?)
Thank you for all the answers.
Multi-routers allows to separate your routes in modules. It avoids to have a big file with all routes. For example, if you take app.use('/users', users), your users.js file will contain all routes associated to users.
Personally, I work on a project with mongoose (http://mongoosejs.com/) which use this architecture :
express/
index.js <- Main file to configure express router with adding middlewares, etc.
routes.js <- All your routes are required in this file
entities/
users/
users.model.js <- Model of the user
users.controller.js <- Controller associated to the model. Any route call a function of the controller
users.routes.js <- All routes for users
projects/
projects.model.js
projects.controller.js
projects.routes.js
With a multi-routers architecture, you will gain scalability and readability. And separation between callbacks and routes avoid to pollute your router with controllers code.
Related
I was wondering what would be the best way to import routes from app_02 to app_01 which is the shell app. One way could be adding children routes but I was wondering if there would be another way to do that so that we can do that so that we don't have to import the components again into the shell app. The main aim of this would be to be able to import different packages having their internal routes into the shell from a config that's in a database.
I'm building an app that should run inside of an AngularJS (1.3.19) module.
The motivation is to reuse AngularJS' already built services and controllers by the scope() and injector() functions.
I cannot use ng-upgrade because this will require refactoring a lot of our legacy code.
Is there a way I can combine two routers for two different AngularJS modules?
We use the angular-ui-router for our AngularJS app.
When I try to change a route on the Angular router, AngularJS' router removes the route and replaces it with an empty string (and runs the otherwise() function as configured).
Any ideas?
Thanks!
I got this.
Configure your paths on both ends. AngularJS' route should point at the html of your Angular app.
Also, I created a node script that imports Angular's routes and transforms them to be AngularJS' routes.
I did this with ts-node and webpack-shell-plugin.
Thanks.
I have 3 modules: SimpleCalculations, FunnyCalculations, OtherCalculations.
Each of them has routing.module.ts file, where I define their child routes and other stuff.
I want to have MainCalculation module, which child routes wold be taken from modules I've already told.
And for example I'd add common CanActivate guard to all routes in these modules.
What the right way to do it? I don't want to mess all this routes in one file.
I'm new to NodeJS and Express moving from PHP. I have been following online tutorials on how to build web apps with Express and it has been an eye opener. So I decided to work on a project with just JavaScript. Problem is after adding a few route definitions in Express, I'm having trouble keeping track of my Express routes and fear it'll get even larger with time. Currently I have almost 45 lines (yes, I have a lot of route, some serve HTML templates files and other just return JSON for my Angular frontend). Is there any better way to manage this before it gets out of hand?
One possible way of organizing the routes is to groups of routes based on functionality and then create separate modules for these groups and then, import these modules to the main app file(app.js or server.js). For example, I am working on a project that has trading functionality, cash deposit functionality and then some other common tasks. So, I have created different modules for these routes and put them in a separate directory called routes.
For example, I have a module named site.js for all the common tasks.
module.exports = function(express,app,passport,router){
router.get('/auth/facebook',passport.authenticate('facebook'));
//other commin routes
};
and in my app.js file, I declare the router and then pass in to the modules that contain routes.
var router = express.Router();
app.use('/',router);
require('./routes/site.js')(express,app,passport,router);
If you are using Express version 4, i wrote a module to handle routes in a cleaner less bloated way. Checkout Exroute Module if it fits your current needs and please provide some feedback if it doesnt.
I have several AngularJS apps that they all have some common dependencies like angular-translate. All the angular apps have the same configuration (in app.js) for angular-translate.
I am looking for a way to externalize the configuration of angular-translate in all these apps. In a way that I will make the changes in one place (maybe a service?) and then the configs will be applied to the apps.
Btw, I am new to Angular world, your precise suggestions would be appreciated.
You can create an angular module config function in a separate project that all of your projects in your projects import and use. Angular allows you to have as many module().config functions as you want. The same goes for .run functions.
Example
// Some common file available to all projects
angular.module('common-config', [
'angular-translate'
/* other common dependencies here */
]).config(['$translateProvider',
function ($translateProvider) {
$translateProvider.preferredLanguage('en');
// Other configuration here.
}]);
// App 1
angular.module('app-one', ['common-config']).run(/*...*/);
// App 2
angular.module('app-two', ['common-config']).run(/*...*/);
// App 3
angular.module('app-three', ['common-config']).run(/*...*/);