简体   繁体   中英

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);                                                                         
}      

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');
  ...
};

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;
}

Two easy ways to achieve what you want:

1. Accessing your shrinkroute instance from within your route

Simple as that. Nothing else is required after Shrinkroute is setup.

exports.showOrListUsers = function(req, res, next) {
  var shrinkr = req.app.shrinkroute;
  console.log( "Route: " + req.route.name ); // ta-da, made available by Shrinkroute
  // do your URL buildings
};

2. Using the middleware

If you don't want be tempted with non URL building methods of Shrinkroute, you can use the middleware, which will make available to you some helpers in your route and in your template (via locals):

// app.js
app.use( shrinkr.middleware );

// routes/index.js
exports.showOrListUsers = function(req, res, next) {
  console.log( "Route: " + req.route.name ); // ta-da, made available by Shrinkroute

  req.buildUrl( "users", { name: "foo" } );
  // or, if you want the full url with the scheme and host...
  req.buildFullUrl( "users", { name: "foo" } );
};

And maybe you want to use them in your templates as well?

// templates/index.jade
a( href=url( "users", { name: "foo" } ) ) Foo profile
a( href=fullUrl( "users", { name: "foo" } ) ) Foo profile

This method has the advantage that you don't get direct access to route setters inside a route.


Disclaimer: I'm the author of Shrinkroute.

you should import it. add following line to the very beginning of your code

  var shrinkroute = require('shrinkroute');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM