node.js - nodejs, how to using module.exports to set multiple parameters in express router -


for example:

app.js:

var express = require('express'); var app = express();  var routestemp=require('./routes/temp'); var routestempexport=require('./routes/temp_export');  app.set('views', path.join(__dirname, 'views')); app.set('view engine', 'mustache');  app.engine('mustache', require('hogan-middleware').__express);   app.use('/',routestemp); app.use('/',routestempexport); module.exports = app; 

/routes/temp.js:

var express = require('express'); var router = express.router();  router.get('/temp',function(req, res, next){    //how set object,function...etc in there module.exports    res.end(); });    // or there set module.exports = router;  

/routes/temp_export.js:

var express = require('express'); var router = express.router();  var getparameters = require('./temp');      router.get('/temp_export',function(req, res, next){     //and how use getparameters multiple;     res.end() });   module.exports = router; 

i tried change module.exports format example:

module.exports = {router:router,obj1:obj1}; module.exports = [router,obj1]; module.exports = router,obj1; 

but did not succeed, , lead router can not identified

if set in module:

// in mymodule.js module.exports = {router:router,obj1:obj1}; 

then, can access both variables upon import by:

const mymodule = require('mymodule.js'); console.log(mymodule.router); console.log(mymodule.obj1); 

you have make absolutely sure 2 exported properties set when export. if set sometime in future via async operation, have no idea if set when want use them.


in looking @ comments in code more, appears you're trying use value computed in route in temp.js, in separate route in temp_export.js. can't directly because routes come sorts of users can't store state 1 route in sort of global on server , expect other route access state (well, could, wouldn't work of time).

instead, typically store state in way identified specific client. then, when particular client makes next request , route gets request, route handler can check client-specific store see if there state there. note creating state in first place violates principles of rest design first design idea should how avoid doing @ all. better put state webpage returned first route , when makes next web request, can include state in query parameter.

the usual places can store client-specific state are:

  1. by putting returned web page (so client can pick state send next request - query parameter or in form post).

  2. by putting state cookie. cookie values available server upon next request.

  3. by storing in server-side session uniquely tied browser/user. server-side session state particular user available server upon next request.

remember more stateless server better (in general). so, if there temporary state relevant particular user, you'd stored in browser , presented server next request, rather having server try keep track of in between requests.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -