express api using one index.js that points to many route files

28 views
Skip to first unread message

Tito

unread,
Feb 14, 2018, 2:55:33 PM2/14/18
to Angular and AngularJS discussion
Greetings,

I want to use one index file that is called from my main app.js

routes = require('./routes')(app);

The index.js in that routes folder calls

module.exports = function(app) {
require('./virtualmachines')(app);
require('./hardwares')(app);
};

but only api calls from virtualmachines are coming with results when I do

curl localhost:8080/api/virtualmachines

What am I missing here. Why only the first route loads?

Thanks much!
routing.png

Zlatko Đurić

unread,
Feb 15, 2018, 3:51:34 AM2/15/18
to Angular and AngularJS discussion
What do you do in those files with the app?

Arnaud Deman

unread,
Feb 15, 2018, 9:07:27 AM2/15/18
to Angular and AngularJS discussion
Hi Tito,
If what you want to do is to organize your routes in several files, say one per URI you could try something like this:

In your files virtuamachines.js and  hardware.js
var express = require('express');
var router = express.Router();

var routes = {
 
get: function(req, res, next) {
               
(....)
 
}
 
//... post, put, delete, etc.
}

router.get('/:name/', routes.get);
module.exports = {routes: routes, router: router};


And then in index.js


var express = require('express');
var virtualmachines = require('./routes/virtualmachines').router;
var hardwares = require('./routes/hardwares').router;

app
.use('/virtualmachines/', virtualmachines);
app
.use('/hardwares/', hardwares);


Regards,
Arnaud.

Tito

unread,
Feb 15, 2018, 4:24:38 PM2/15/18
to Angular and AngularJS discussion
hey Zlato, great question! :) nothing I guess which I think gives me a hint as to what I need to do

Thanks!

Tito

unread,
Feb 15, 2018, 4:51:45 PM2/15/18
to Angular and AngularJS discussion
thanks a lot!

Tito

unread,
Feb 23, 2018, 11:15:06 AM2/23/18
to Angular and AngularJS discussion
Hello

This is what I Was looking for


So that when I plug in a new service folder it is aware and picks that without me having to specify a new entry everytime I have a new api endpoint

THanks!

Arnaud Deman

unread,
Feb 24, 2018, 1:14:24 PM2/24/18
to Angular and AngularJS discussion
Hi Tito,
Interesting, thanks or sharing.
Arnaud.
Reply all
Reply to author
Forward
0 new messages