NodeJS consume functions that have parameters (res, req) from another file.js

0

I have the following problem, which I think is very simple, but I am very new to node, so I still have problems with these things.

I have a .js file that connects to an external service and makes use of (res and req) and returns it as json.

What I need is to return that result inside a variable in another .js file

I'll give you an example so you can understand me.

    export function getConversation( req, res) {
  //  Getting workspace credentials from mongo
  //connectToConversation('req.body.conversationName');
  //console.log(input);
  connectToConversation('Courtesy'); //Business
  conversation = new Conversation({
    username: CurrentConversation[0]['username'],
    password: CurrentConversation[0]['password'],
    url: '',
    version_date: 
  });
  console.log(req.body.context);
  var workspace = CurrentConversation[0]['workspaceID'];
  //var workspace = '';
  if (!workspace) {
    return res.json({
      'output': {
        'text': ''
      }
    });
  }
  console.log(CurrentConversation[0]['workspaceID']);
  var payload = {
    workspace_id: workspace,
    input: req.body.input || {}
  };


  conversation.message(payload, function(err, data) {
    if (err) {
      return res.status(err.code  || 500).json(err);
    }
    return res.json(updateMessage(payload, data));
  });
}

I need to use that function from another file.js and send the result to it.

I know the problem is simple, sorry for bothering.

Thank you.

    
asked by Alejandro Sanchez 17.05.2017 в 23:24
source

1 answer

0

I have him in a folder called router and this is an example as I have it:

router.js

var express = require("express");
var passport = require("passport");
var contenido = require("./contenido");

var router=express.Router();

//Así directamente
router.get("/menu/todos",contenido.mostrartodos);

//o de esta forma 
router.get("/menu/todos", function (req,res) {
  contenido.mostrartodos (req, function (result) {
    res.json(result);
  })
})

contenido.js

//Así directamente
module.exports.mostrartodos = function (req, res) {
  res.json({result: 'resultado'});
}

//o de esta forma

module.exports.mostrartodos = function (req, callback) {
  var resutl = {
    result: 'resultado'
  }
  callback(result);
}
    
answered by 17.05.2017 в 23:52