Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass variable from app.js to routes/index.js?

I'm using shrinkroute https://npmjs.org/package/shrinkroute to make links in nodejs. I get error 500 ReferenceError: shrinkr is not defined

How to pass shrinkroute to routes/index.js? Is there a better way to create url by passing query string args?

//app.js
var app = express();

var shrinkr = shrinkroute( app, {
    "user": {
        path: "/user/:id?",
        get: routes.showOrListUsers
    }
});
//url method works in app.js    
var url = shrinkr.url( "user", { id: 5, page:40, type:'a' } );
console.log(url);

app.use( shrinkr.middleware );

//routes/index.js
exports.showOrListUsers = function(req, res, next) {                       
    console.log(req.params); 
    //shrinkr errors out in index.js                                      
    var url2 = shrinkr.url( "users", {name: "foo"});                       
    console.log(url2);                                                                         
}      
like image 738
Vlad Vinnikov Avatar asked Dec 20 '13 22:12

Vlad Vinnikov


People also ask

How do you store local variables that can be access within the application in Express JS?

We can store local variables that can be accessed within the application by using app. locals.

How do I define a global variable in Express?

To set up a global variable, we need to create it on the global object. The global object is what gives us the scope of the entire project, rather than just the file (module) the variable was created in. In the code block below, we create a global variable called globalString and we give it a value.

How does routing work in Express JS?

A route method is derived from one of the HTTP methods, and is attached to an instance of the express class. The following code is an example of routes that are defined for the GET and the POST methods to the root of the app. Express supports methods that correspond to all HTTP request methods: get , post , and so on.


2 Answers

One solution would be to store shrinkr in your app object using app.set:

// app.js
...
app.set('shrinkr', shrinkr);
...

In routes/index.js, you can access it through the req.app or res.app objects:

exports.showOrListUsers = function(req, res, next) {
  var shrinkr = req.app.get('shrinkr');
  ...
};
like image 89
robertklep Avatar answered Sep 28 '22 09:09

robertklep


A bit late to the party, but the following works as well:

app.js

var my_var = 'your variable';

var route = require('./routes/index')(my_var);
app.get('/', route);

and meanwhile in route.js

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

// Router functions here, as normal; each of these
// run only on requests to the server

router.get('/', function (req, res, next) {
    res.status(200).end('Howdy');
});


module.exports = function(my_var){

    // do as you wish
    // this runs in background, not on each
    // request

    return router;
}
like image 25
Andrew Avatar answered Sep 28 '22 09:09

Andrew