在导出模块

时间:2017-01-18 16:13:45

标签: javascript node.js

我想在modules.exports中传递多个参数。我的代码是这样的:

以下文件是index.js的代码片段

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

以下是routes.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");

});

}

问题是,当发出POST请求时,console.log(number);在终端上打印undefined,当我在{{1}中用number替换app时打印它打印console.log()的所有功能。

我还尝试切换appapp的位置,然后抛出number不存在的错误。

2 个答案:

答案 0 :(得分:1)

如果您只是想传递变量,为什么不将它简单地添加到app对象。

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

然后在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");

});

}

编辑:

以下代码将用于传递变量:

<强> app.js:

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

要在函数内部传递变量,您必须将其绑定。

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

}

答案 1 :(得分:0)

不要这样做!使用expressJS的路由器,也许包裹Consign一次传递多个路由文件......

using System;
using System.Collections.Generic;
using System.Web.UI;

public partial class _Default : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(this.IsPostBack) return;
        ExpandableSelections items = new ExpandableSelections();
        if(items.Count == 0) return;
        this.gvSelectionList.DataSource = items;
        this.gvSelectionList.DataBind();
    }
    protected string GetSerialNumberText(object dataItem)
    {
        SerialNumberData item = (SerialNumberData)dataItem;
        return item.SerialNumber;
    }
}
public class SerialNumberData
{
    public string SerialNumber { get; set; }
    public SerialNumberData(string data) { SerialNumber = data; }
}
public class ExpandableSelections : List<SerialNumberData>
{
    internal ExpandableSelections()
    {   // Emulate database call
        for (int i = 1; i < 72000; i++)
            this.Add(new SerialNumberData("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"));
    }
}

然后:

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;