0

Sorry ahead of time if this sounds confusing, or has been answered before.

Using node.js, express and express-generator, I created an app that has two route files:

  1. The first route file does MySQL database interaction and returns the result of queries (used for displaying data on the home and admin pages).
  2. The second route file uses an LDAP server for checking login credentials, as well as managing login sessions (used for checking authorization on the login and admin pages).

Both routes requires express to function, but if I require express in both route files it gives me an error. Is there a way to allow both route files to use the same express router?

app.js

var express = require('express');
// Other generated requires

var data = require('./routes/data');
var auth = require('./routes/auth');

var app = express();

// Other generated sets and uses

app.use('/', data);
// "login" and "admin" need to use both data.js and auth.js  <----------------
//app.use('/login', data);
//app.use('/login', auth);
//app.use('/admin', data);
//app.use('/admin', auth);

module.exports = app;

routes/data.js

var express = require('express');
var router  = express.Router();

// Database functions

router.get('/', function (request, response) {
    response.render('index');
});

module.exports = router;

routes/auth.js

var express   = require('express');
var session   = require('express-session');
var directory = require('activedirectory');
var router    = express.Router();

// Login and session functions

router.get('/login', function (request, response) {
    // Login and session check
    response.render('login');
});

router.get('/admin', function (request, response, next) {
    // Session check
    response.render('admin');
});

module.exports = router;
Joseph Webber
  • 2,010
  • 1
  • 19
  • 38
  • "if I require express in both route files it gives me an error" What is the error? – Michelle Tilley May 19 '15 at 15:38
  • possible duplicate of [How to include route handlers in multiple files in Express](http://stackoverflow.com/questions/6059246/how-to-include-route-handlers-in-multiple-files-in-express) – Jordan Running May 19 '15 at 15:42
  • To clarify, this strategy works just fine (calling `app.use("/subpath", router)`), so there's something else going on. – Michelle Tilley May 19 '15 at 15:52
  • @BinaryMuse Sorry, I was checking other solutions. I get a 404 error when I try and access the login and admin pages. – Joseph Webber May 19 '15 at 16:01
  • @JosephWebber Something else is up then; this gist shows a working example: https://gist.github.com/BinaryMuse/64b14ce99adddb90b700 – Michelle Tilley May 19 '15 at 16:10
  • @BinaryMuse I don't think `response.json` will work for me since express-generator uses .jade files that need to be rendered before JSON is sent to them. – Joseph Webber May 19 '15 at 16:17
  • @JosephWebber `response.json` is just a simple way to make sure the route handler is correctly running. – Michelle Tilley May 19 '15 at 16:18
  • @JosephWebber I apologize if this is silly, but I have to ask—did you uncomment the four lines in `app.js` that `use` the required routers? – Michelle Tilley May 19 '15 at 16:19
  • @BinaryMuse Hehe, yes I did. Nothing silly about it, if anything I'm the silly one for not un-commenting them when I pasted my code. – Joseph Webber May 19 '15 at 16:25

0 Answers0