Abhishek Parikh Abhishek Parikh - 20 days ago 10
JSON Question

pass parameter function Node.js from one js to another

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

Here is what I am trying.
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

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.

Answer

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

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')
Comments