简体   繁体   English

如何从Node.js中的模块导出函数

[英]How to export functions from modules in node.js

I am using node.js (v4.2.2) with express (4.13.1) . 我正在使用express(4.13.1)的 node.js(v4.2.2 I am trying to import my custom module functions to another module. 我正在尝试将自定义模块功能导入另一个模块。 Application is created with express, and only thing added to app.js is require for my route var tests = require('./routes/tests'); 应用程序是使用express创建的,而对我的route var tests = require('./routes/tests');唯一需要添加到app.js中的东西var tests = require('./routes/tests'); and app.use for that route app.use('/tests', tests); 和该路线的app.use('/tests', tests);

My two custom files (modules) are (path is relative to project root): 我的两个自定义文件(模块)是(路径相对于项目根目录):

  • ./model/test.js ./model/test.js
  • ./routes/tests.js ./routes/tests.js

Here is ./model/test.js : 这是./model/test.js

var id;
var testNumber1;
var testNumber2;

function Test(id, testNumber1, testNumber2) {
    this.id = id;
    this.testNumber1 = testNumber1;
    this.testNumber2 = testNumber2;
};

exports.reset = function() {
    this.testNumber1 = 0;
    this.testNumber2 = 0;
};

module.exports = Test;

And here is ./routes/tests.js : 这是./routes/tests.js

var express = require('express');
var Red = require('../model/test.js');
var router = express.Router();

/*create new test :id*/
router.post('/:id', function(req, res, next) {
  var myNewTest = new Red(req.params.id, 0, 0)
  myNewTest.testNumber2 += 1;
  myNewTest.reset();
  res.send('id: ' + myNewTest.id + 
    ' testNumber2: ' + myNewTest.testNumber2);
});

module.exports = router;

When I try to execute curl -X POST http://localhost:3000/tests/1 i get error TypeError: myNewTest.reset is not a function . 当我尝试执行curl -X POST http://localhost:3000/tests/1出现错误TypeError: myNewTest.reset is not a function I am having trouble understanding how to export functions correctly. 我在理解如何正确导出功能时遇到了麻烦。 If I understand this api reference correctly, to expose constructor of module, i have to use module.exports = Test; 如果我正确理解此api参考module.exports = Test;公开模块的构造函数,必须使用module.exports = Test; , but that doesn't expose reset function. ,但这并没有公开reset功能。 So, to expose it I have declared it like exports.reset = function() {...} , but obviously, that doesn't work, at least not in my case. 因此,要公开它,我已将其声明为exports.reset = function() {...} ,但显然,这是行不通的,至少在我看来是这样。

Through some other answers I have also seen function being declared normally function reset() {...} , and exposed like exports.reset = reset; 通过其他一些回答,我还看到函数通常被声明为function reset() {...} ,并且像exports.reset = reset;一样暴露exports.reset = reset; , which gives me the same error. ,这给了我同样的错误。

How do I expose reset function properly? 如何正确显示重置功能?

You should add it to the prototype, at the moment it's just a static method in your module, not attached to the Test constructor. 您应该将其添加到原型中,此刻它只是模块中的静态方法,而不是附加到Test构造函数。

function Test(id, testNumber1, testNumber2) {
    this.id = id;
    this.testNumber1 = testNumber1;
    this.testNumber2 = testNumber2;
};

Test.prototype.reset = function() {
    this.testNumber1 = 0;
    this.testNumber2 = 0;
};

module.exports = Test;

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

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