简体   繁体   English

在导出模块中传递多个参数

[英]passing multiple arguments in export module

I want to pass multiple argument in modules.exports . 我想在modules.exports传递多个参数。 My code goes something like this: 我的代码是这样的:

Following file is a code snippet of index.js 以下文件是index.js的代码段

var express        = require('express');
var app            = express();
var number = 100;
require('./app/routes')(app, number); // pass our application into our routes

Following is a code snippet of routes.js 以下是route.js的代码段

module.exports = function(app, number){

app.get('/api/control', function(req,res){
  res.send("done");
});

app.post('/api/control', function(req,res){
  console.log(number);
  console.log(req.body);
  res.send("done");

});

}

The problem is when ever a POST request is made, console.log(number); 问题是,无论何时发出POST请求, console.log(number); prints undefined on terminal where as when I print when I replace number with app in console.log() it prints all the functions of app . 在终端上打印undefined ,就像我在console.log()中用app替换number时打印时一样,它将打印app所有功能。

I also tried to switch the positions of app and number ,then it was throwing an error that app does not exists. 我还试图切换appnumber的位置,然后抛出一个错误,提示该app不存在。

If you just want to pass the variable why not simply add it to the app object. 如果您只想传递变量,为什么不简单地将其添加到app对象。

var express        = require('express');
var app            = express();
app.myNumber = 100;
require('./app/routes')(app); // pass our application into our routes

Then in the routes.js: 然后在routes.js中:

module.exports = function(app){

app.get('/api/control', function(req,res){
    res.send("done");
});

app.post('/api/control', function(req,res){
    console.log(app.myNumber);
    console.log(req.body);
    res.send("done");

});

}

Edited: 编辑:

The following code will work to pass variables: 以下代码可用于传递变量:

app.js: app.js:

var express        = require('express');
var app            = express();
var number = 100;
require('./app/routes')(app, number);

To pass the variable inside the function, you will have to bind it. 要将变量传递到函数内部,必须将其绑定。

module.exports = function(app, number){

  app.get('/api/control', function(req,res){
    res.send("done");
  });

  app.post('/api/control', function(req,res){
      console.log(number);
      console.log(req.body);
      res.send("done");

  }.bind(number));

}

Do not do in that way ! 不要那样做! Use the Router of expressJS and maybe the package Consign to pass multiple route file at once... 使用expressJS的路由器以及包Consign一次传递多个路由文件...

 const express = require('express'); const router = express.Router(); /* GET login page. */ router.get('/', function(req, res, next) { res.render('login'); }) router.post('/', function (req, res, next){ if (req.body.code === '***') { res.send({ status: 'OK', token: *** }); } else { res.send('KO'); } }) module.exports = router; 

Then: 然后:

 const loginRoute = require('./routes/login'); app.use('/', loginRoute); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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