H
H
Hazrat Hajikerimov2017-01-08 12:52:49
Node.js
Hazrat Hajikerimov, 2017-01-08 12:52:49

How to combine routes in nodejs?

I started learning Node.js and wondered how you can combine routes in Node? Now I'll explain what I mean:
Application routes look something like this:

var controllers = require('./controllers'),
    myApp = require('../app'),
    admin = require('../admin');

// Основной роутер
module.exports = function (app) {

    // Отдаем сайт
    app.get('/', myApp.run);

    // Отдаем админ-панель
    app.get('/admin', admin.run);

    // RESTful controllers
    controllers(app);

    // Если нет обработчиков, 404
    app.get('*', function (req, res) {
        res.status(404).send("<h1>Page Not found</h1>");
    });
};

Here we are only interested in it. controllers(app);
It looks like this:
var navigation = require('./navigation'),
    page = require('./page'),
    publication = require('./publication'),
    setting = require('./setting'),
    user = require('./user');

module.exports = function (app) {

    // Navigation controller routes
    navigation(app);

    // Page controller routes
    page(app);

    // Publication controller routes
    publication(app);

    // Setting controller routes
    setting(app);

    // User controller routes
    user(app);

};

Where each controller handles routes like this:
var page = require('../../controllers/page');

module.exports = function (app) {
    app.get('/api/page', function (req, res) {
        res.send(page.page());
    });
};

That is, all requests go through the url /api/"controller"/"method"
Now the question is, is it possible somehow to make it possible to somehow determine in the root router that the url of this type /api/*if so, then call the controller router .
It seems to me that if the requests are not to the api, then why does node'e go through the intermediate handlers of the controllers? or is it saving on matches (since node won't run an intermediate handler if the url doesn't match)?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
Pavel, 2017-01-08
@Volde

const apiRouter = require('./api/api');
app.use('/api', apiRouter());

api.js containing routes
const express = require('express');
const router = new express.Router();
const users = require('../../controllers/users');
const pages= require('../../controllers/pages');

module.exports = () => router
  .get('/pages', pages.getPages)
  .get('/user', users.getUsers);

O
Oleg Kukil, 2017-01-12
@flashbag

server.js

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

var fs = require("fs");
var path = require("path");

module.exports = function (server) {

  fs.readdirSync(path.resolve(__dirname)).forEach(function (file) {
    if (file.substr(-3, 3) === '.js' && file !== 'index.js') {
      var full_path = path.resolve(__dirname) + '/' + file.replace('.js', '');
      // console.log(full_path);
      require(full_path)(server);
    }
  });

};

var testController = require('../controllers/test');

module.exports = function(server) {

  server.get('/test', testController.test);

};

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question