2

How could I split the routing into different files?

This what I tried, but did not work:

// file 'index.js' as main in manifest.json
const createRouter = require('@arangodb/foxx/router');  
const router = createRouter();
const entries = require('./routes/entries')
entries.init(router);
module.context.use("", router);

and the entries file is working as a function:

// file './routes/entries.js'
const db = require('@arangodb').db;
// [...] more const
module.exports = {
  init: function(router) {

    router.post('/entries', function(req, res) {
    // [...] post handle
    }
  }
}

1) How could I modify the router in a js file and reuse in?

module.context.use(router)

2) Any idea how to handle all js files in the folder 'routes' to define the router and so minimize the definition for route files?

Hayo
  • 149
  • 8

1 Answers1

1

you can use the funtion router.use([path], middleware, [name]): Endpoint for that.

module.context.use('/entries', require('./routes/entries'), 'entries');

For more information take a look into the docs here or in the newest Foxx tutorial here which also use child routers.

mpv89
  • 1,891
  • 9
  • 10
  • Thanks, but how about having more than one entries.js file adding to it? Can I use: module.context.use('/entries', require('./routes/**'), 'entries'); – Hayo Sep 20 '17 at 08:50