Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7912079
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T13:29:13+00:00 2026-06-03T13:29:13+00:00

Can you simply set all routing configurations in a single object? Basically what I’d

  • 0

Can you simply set all routing configurations in a single object? Basically what I’d like to do is have a file called routing.js that exports an object containing all of the routes. Having to use express.get() for every route is mundane.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T13:29:14+00:00Added an answer on June 3, 2026 at 1:29 pm

    I actually have a folder that holds all the files for my routes (I split them up by object). I also separate out my controllers into files as well. The route files then look like this:

    /**
     * Module dependencies.
     */
    var controller = require('../controllers/index');
    
    /**
     * The API root for this object.
     */
    
    var root = '/';
    
    /**
     * Route definitions.
     */
    var routes = function (app) {
    
      // get index
      app.get(root, controller.getIndex);
    };
    
    /**
     * Exports.
     */
    module.exports = routes;
    

    Then in my app.js file I do this which automatically loads up all of my route files and configures the 500 and 404 routes:

    fs.readdir(__dirname + '/routes', function (err, files) {
      if (err) throw err;
      files.forEach( function (file) {
        require('./routes/' + file)(app);
      });
    
      /**
       * 500 page.
       */
      app.use( function (err, req, res, next) {
    
        if (err && err.status === 404) {
          return next();
        }
    
        res.statusCode = 500;
        res.render('404.html', {status: 500, url: req.url, error: ': ' + err.message});
      });
    
      /**
       * 404 page.
       */
      app.use( function (req, res, next) {
        res.statusCode = 404;
        res.render('404.html', {status: 404, url: req.url});
      });
    });
    

    If you don’t want to read the files in like this, the import part is this line:

    require('./routes/' + file)(app);
    

    Which allows you to use the route that was defined previously.

    I do it this way to maintain control over exactly which routes are created. If you want to automate the process you can use https://github.com/visionmedia/express-resource.

    This allows you to do:

    var express = require('express'), 
        Resource = require('express-resource'),
        app = express.createServer();
    
    app.resource('forums', require('./forum'));
    

    In order to create automatically routes that point to the following functions in ./forum:

    GET     /forums              ->  index
    GET     /forums/new          ->  new
    POST    /forums              ->  create
    GET     /forums/:forum       ->  show
    GET     /forums/:forum/edit  ->  edit
    PUT     /forums/:forum       ->  update
    DELETE  /forums/:forum       ->  destroy
    

    If you want something even simpler, you could do this…

    var getRoutes = [['/', controller.index], ['/post', controller.showPost]];
    
    for (var i = 0, len= getRoutes.length; i < len; i++) {
       var path = getRoutes[i][0];
       var controller = getRoutes[i][1];
       app.get(path, controller);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a git repository set up that looks something like this: PROD /
I hate generating an exception for things that I can simply test with an
When you declare a type parameter for the class, you can simply use that
With all of the fundamental types of C++, one can simply query: if(varname) and
Using PHP5, how can I use the eBay API to set all my 2000+
Hi I know that I can set the rest authentication in Phil Sturgeons rest
If you're setting all the borders to be the same, you can simply do
I've set up a very simple example with LINQ-TO-SQL in WPF. I can get
I'm trying to set up a simple Roles model for my site. Users can
I'm trying to make an applet which I can simply drag an image. And

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.