A
A
Anton Ulanov2016-03-16 00:41:14
Node.js
Anton Ulanov, 2016-03-16 00:41:14

How to put routes into separate files and include them?

Good time of the day, I’m learning hapi.js and I couldn’t master the transfer of route into separate files and connect them, more precisely 1 route could take out and connect and the rest are not given :( Please tell me how to implement it correctly

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Markus Kelvin, 2016-03-16
@antonsr98

Create separate files in the folder routes/route1.js, routes/route2.js and so on and export their modules.

// routes/route1.js

module.exports = [
    { 
      method: 'GET', path: '/routes1', 
      handler: function () {} },
    { 
      method: 'GET', path: '/routes1/{id}', 
      handler: function () {} }
];

then in the routing folder create index.js where to connect all route1.js, route2.js, route3.js......routeN.js and export them all in an array
//  /routes/index.js

var route1 = require('./route1');
var route2 = require('./route2');
var route3 = require('./route3');
var routeN = require('./routeN');

module.exports = [].concat(route1, route2, route3, routeN);

and now you can call this entire array in the server file and transfer the array from the routers to the server.
//  /server.js

var routes = require('./routes');
server.route(routes);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question