2

How can I pass an array of objects to the routes middle-ware so that I can render the view depending on the variables passed.

Currently, this is the layout I am working with

app.js

var index = require('./routes/index');

ARRAY = [objects];
app.use('/', index);

routes/index.js

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

router.get('/', function(req, res, next){
  res.render('index', {data: ARRAY});
});

module.exports = router;

index.jade

extends layout
block content
  each d in data
    div!= d.method()

Currently the only solution I can produce that actually works is to use global variables, but this is not sustainable or scalable. I have tried app.set('shrinkr', shrinkr); and app.locals but cannot get either of these to work with the layout of my app. From my understanding of the Express4 framework I should be able to do something like this in app.js:

var index = require('./routes/index')(array);

but I cannot get the implementation correct, and there is no info about this on the web.

1 Answer 1

2

There are multiple ways that you can tackle this, but if you want to "bake" the array into the middleware (as your code sample suggested), you can do this:

var express = require('express');

module.exports = function(array) {
    var router = express.Router();
    return router.get('/', function(req, res, next){
      res.render('index', {data: array});
    });
}

Then you can use it as you suggested:

var index = require('./routes/index')(array);

Because new middleware is created every time you invoke the exported function, you can bake independent arrays into different middleware:

var index1 = require('./routes/index')(["array 1"]);
var index2 = require('./routes/index')(["array 2"]);

Which may or may not be useful to you, depending on your needs.

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you for your answer - this looks exactly what I need however when I try to implement it with app.use('/', index); I get thrown a 'Router.use() requires middleware function but got a ' + gettype(fn) -- how can I use this implementation with app.use
Sorry, I forgot a return statement; it's been updated, and it should work now.
Okay cheers, that seems to function as expected. Problem I am having now is related to the fact that new middleware is created every time an exported function is invoked. How can I do app.use with a defined route with a consistently changing array? I have a loop which updates an array every 10 seconds, and I want the middleware to update with it, but redefining index in the same loop that updates the array like loop{ array1.push(1); index = require('./routes/index')(array1);} doesn't work.. :(
Hi, William, it works for me. I tried it out with this program: gist.github.com/EthanRBrown/824a776c3405901c47fe427e8c3d9338
Okay thanks for your help Ethan. I wrote a similar debug app and it worked, must be a problem with how my app is structured. Cheers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.