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 5952607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:42:20+00:00 2026-05-22T17:42:20+00:00

I have an NodeJS Express app that is getting really big in just one

  • 0

I have an NodeJS Express app that is getting really big in just one file (app.js).

I want to export all my routes into a single, external file, say ./lib/routes.js. How to do that?

How to export the following bit of code and require it correctly in main app.js?

app.get('/logout', function(req, res) {
    res.render('logout', {
        username: req.session.username
    });
});

app.get('/dashboard', function(req, res) {
    res.render('dashboard', {
        username: req.session.username
    });
});

app.get('/login', function(req, res) {
    res.render('login', {
        badLogin: false,
        loginError: false
    });
});
  • 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-05-22T17:42:21+00:00Added an answer on May 22, 2026 at 5:42 pm

    Why not do something like this:

    // logout.js
    module.exports = function(req, res){
      res.render('logout', {
        username: req.session.username
      });
    });
    
    // dashboard.js
    module.exports = function(req, res){
      res.render('dashboard', {
        username: req.session.username
      });
    });
    
    // login.js
    module.exports = function(req, res){
      res.render('login', {
        badLogin: false,
        loginError: false
      });
    });
    
    // app.js
    app.get('/logout', require('logout'));
    app.get('/dashboard', require('dashboard'));
    app.get('/login', require('login'));
    

    Also, you could imagine easily using http://nodejs.org/docs/v0.4.8/api/fs.html#fs.readdir to loop through a routes directory and load these up programmatically.

    You could even do something along the lines of…

    module.exports.handler[] = {
        method : 'get',
        route  : '/login',
        action : res.render('login', {
           badLogin: false,
           loginError: false
        });
    }
    

    Though I think I’d spend a little time thinking about how to simplify that.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assume that we have N erlang nodes, running same application. I want to share
I have a bunch of MDI child nodes that are all created in the
I have 3 home PCs that have 2008 SQL Server Express instances installed on
I have an existing app written in PHP (using Kohana framework) and I want
I'm writing a Java Tree in which tree nodes could have children that take
Is it possible to use XPath to select only the nodes that have a
I have a treeview with nodes like this: Foo (1234), and want to allow
I have several templates that match various nodes in an xml document. If I
I have a nodejs TCP server and a client. Basic network communication happens. Client
I have a sea of weighted nodes with edges linking clusters of nodes together.

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.