4

I have a directory structure which looks somewhat like this (both folders have node_modules available)

- task1
     - src
          - shorten.js
- task2
     - src
          - api.js
          - server.js

In shorten.js I have two functions namely shorten(url) and checkLink(url) . At the end of the file, I have something as module.exports = shorten.

In the api.js, I have a line const shorten = require("../../task1/src/shorten");. If I simply call shorten with the parameter, it is having no problems, but the problem comes when I try to call checkLink in a similar way.

What should I do in order to be able to call checkLink inside api.js of task2?

Sparker0i
  • 1,787
  • 4
  • 35
  • 60
  • Possible duplicate of [In Node.js, how do I "include" functions from my other files?](https://stackoverflow.com/questions/5797852/in-node-js-how-do-i-include-functions-from-my-other-files) – Aramil Rey Jul 26 '18 at 14:07
  • @AramilRey I tried that by creating a var that holds a function, it did not work – Sparker0i Jul 26 '18 at 14:18

3 Answers3

9

You need to also export the checkLink function inside the shorten.js so you can then require it from the api.js...

Inside shorten.js change your module.exports to look like this:

module.exports = { shorten, checkLink }

Inside the api.js like this:

let myShortenFile = require ("../../task1/src/shorten")
myShortenFile.shorten() // to call shorten
myShortenFile.checkLink() // to call the checkLink

Hope this helps.

// UPDATE:

Since the OP has indicated he cannot export both functions and only wants to export 1 function and still have access to the second...

// shorten.js
const shorten = function(url, isCheckLink){
 if(isCheckLink){
  // Perform check link code
  return Result_of_check_link_function
 }
 else{
  return Result_of_shorten_function
 }
}

module.exports = shorten

// inside api.js
let myShortenFile = require ("../../task1/src/shorten")
myShortenFile.shorten('http://myurl.com') // to call shorten
myShortenFile.shorten('http://myurl.com', true) // pass true 
// as second argument to call the CheckLink function
jremi
  • 2,879
  • 3
  • 26
  • 33
  • Hey, I think I am only allowed to have shorten inside module.exports, else the test cases will fail. Is there no other way we can have it? – Sparker0i Jul 26 '18 at 14:28
  • You could embed the checkLink function inside the exported shorten function and then setup argument that you can use to call the inside checkLink function – jremi Jul 26 '18 at 14:30
0

Currently you are exporting only one function which is module.exports = shorten.

You need to export checkLink(url) as well same as shorten like below:

module.exports = checkLink

Another way to combine both exports like below:

module.exports = {
  shorten,
  checkLink
}

Here is one nice article about Exporting and Importing modules in nodejs.

Vikasdeep Singh
  • 20,983
  • 15
  • 78
  • 104
0

Or simply you can use module.exports

module.exports = {
    shorten: function(url) {
     //some process here
        return {
            success: true,
            data: url
        }
    },

    checkLink: function (any,values,here) {
        //some code
        return { value };
    }
}

In the request use as follow

let someShorten= require("./place/to/file");

someShorten.shorten('http://myurl.com');
Isuru Dilshan
  • 719
  • 9
  • 18