传递参数函数节点。从一个js到另一个

时间:2021-12-16 16:02:44

I am trying to pass function response from one Node.js script to another

我正在尝试从一个节点传递函数响应。js脚本到另一个

Here is what I am trying. Index.js

这是我正在尝试的。Index.js

var express = require('express');

require('./meter');

var app = express();

app.get('/',function(req,res){
    return;                    //hi function from another file
})

app.listen(3000);
console.log('listening on 3000 port')

meter.js

meter.js

module.exports = {

    hi: function(req, res) {
        return res.status(200).json({"message" : "Hello V1"});
    }
};

does require function only will do the job?

只需要函数就可以完成工作吗?

Thanks in advance.

提前谢谢。

2 个解决方案

#1


2  

When you use require, you should assign it to a variable, and then you can use it in your code:

当你使用require时,你应该将它分配给一个变量,然后你可以在你的代码中使用它:

var express = require('express');
var meter = require('./meter');  // meter will be an object you can use later on
var app = express();

app.get('/',meter.hi); // you actually don't need another annonimous function, can use hi directly as the callback

app.listen(3000);
console.log('listening on 3000 port')

#2


2  

The answer by Nir Levy is correct but I'll try to give you a little more context on what's going on.

Nir Levy的答案是正确的,但是我会试着给你们更多的背景知识。

var express = require('express');
// Import the meter export and assign it a variable for reuse.
var meter = require('./meter');
var app = express();
app.listen(3000);
console.log('listening on 3000 port')

As per Nir's answer, you'll just use meter.hi for handling get requests to /

根据Nir的回答,你只需要使用米。你好,这里是get请求

app.get('/', meter.hi);

What exactly is happening here is JavaScript passes all the arguments to the meter.hi method. In case of express there will be 3 arguments here - request, response and next passed in that order.

这里真正发生的是JavaScript将所有参数传递给评测器。你好方法。如果要表达,这里将有3个参数——请求、响应和下一个顺序。

In the module meter you are just using request and response alone, which is fine, but if there's any other processing required or arguments for meter.hi needs to vary you might want to follow the following practise.

在模块仪表中,您只使用请求和响应,这是可以的,但是如果需要进行任何其他处理或为仪表参数的话。hi需要改变你可能想要遵循以下练习。

app.get('/', function( req, res ) {
  // You can process the request here.
  // Eg. authentication
  meter.hi(req, res);
});

Where you have more control over the arguments being passed to your modules.

您可以对传递给模块的参数进行更多的控制。

#1


2  

When you use require, you should assign it to a variable, and then you can use it in your code:

当你使用require时,你应该将它分配给一个变量,然后你可以在你的代码中使用它:

var express = require('express');
var meter = require('./meter');  // meter will be an object you can use later on
var app = express();

app.get('/',meter.hi); // you actually don't need another annonimous function, can use hi directly as the callback

app.listen(3000);
console.log('listening on 3000 port')

#2


2  

The answer by Nir Levy is correct but I'll try to give you a little more context on what's going on.

Nir Levy的答案是正确的,但是我会试着给你们更多的背景知识。

var express = require('express');
// Import the meter export and assign it a variable for reuse.
var meter = require('./meter');
var app = express();
app.listen(3000);
console.log('listening on 3000 port')

As per Nir's answer, you'll just use meter.hi for handling get requests to /

根据Nir的回答,你只需要使用米。你好,这里是get请求

app.get('/', meter.hi);

What exactly is happening here is JavaScript passes all the arguments to the meter.hi method. In case of express there will be 3 arguments here - request, response and next passed in that order.

这里真正发生的是JavaScript将所有参数传递给评测器。你好方法。如果要表达,这里将有3个参数——请求、响应和下一个顺序。

In the module meter you are just using request and response alone, which is fine, but if there's any other processing required or arguments for meter.hi needs to vary you might want to follow the following practise.

在模块仪表中,您只使用请求和响应,这是可以的,但是如果需要进行任何其他处理或为仪表参数的话。hi需要改变你可能想要遵循以下练习。

app.get('/', function( req, res ) {
  // You can process the request here.
  // Eg. authentication
  meter.hi(req, res);
});

Where you have more control over the arguments being passed to your modules.

您可以对传递给模块的参数进行更多的控制。