Abhishek Parikh Abhishek Parikh - 1 year ago 81
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.

var express = require('express');


var app = express();

return; //hi function from another file

console.log('listening on 3000 port')


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 Source

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

console.log('listening on 3000 port')
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download