node js: Why module.exports = tesdf() from require is executed only once, inside get request












0















index.js



'use strict'

const express = require('express')
const app = express()

app.get('/', (req, res, next) => {
require('./test.js') // after repeated request is not executed
console.log("test")//after repeated request is executed
next();
})

app.listen(8097)


test.js



function tesdf(){console.log("Gtest test", ind++); }
module.exports = tesdf()


How to make a function called again.
thank you.










share|improve this question



























    0















    index.js



    'use strict'

    const express = require('express')
    const app = express()

    app.get('/', (req, res, next) => {
    require('./test.js') // after repeated request is not executed
    console.log("test")//after repeated request is executed
    next();
    })

    app.listen(8097)


    test.js



    function tesdf(){console.log("Gtest test", ind++); }
    module.exports = tesdf()


    How to make a function called again.
    thank you.










    share|improve this question

























      0












      0








      0








      index.js



      'use strict'

      const express = require('express')
      const app = express()

      app.get('/', (req, res, next) => {
      require('./test.js') // after repeated request is not executed
      console.log("test")//after repeated request is executed
      next();
      })

      app.listen(8097)


      test.js



      function tesdf(){console.log("Gtest test", ind++); }
      module.exports = tesdf()


      How to make a function called again.
      thank you.










      share|improve this question














      index.js



      'use strict'

      const express = require('express')
      const app = express()

      app.get('/', (req, res, next) => {
      require('./test.js') // after repeated request is not executed
      console.log("test")//after repeated request is executed
      next();
      })

      app.listen(8097)


      test.js



      function tesdf(){console.log("Gtest test", ind++); }
      module.exports = tesdf()


      How to make a function called again.
      thank you.







      node.js get require






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 20 '18 at 12:34









      TflagTflag

      184




      184
























          4 Answers
          4






          active

          oldest

          votes


















          1














          When you are exporting a file, you export the function, classes or variables. Here you are exporting the result of tesdf() function. To solve this just export it like:



          function tesdf(){console.log("Gtest test", ind++); }

          module.exports = tesdf


          Also you should not use required('./test.js') inside of the get request because everytime some client request for that end point, the method is going to load the file. e.g



          let test = require('./test.js');
          app.get('/', (req, res, next) => {
          test();//Call the method
          console.log("test")//after repeated request is executed
          next();
          })





          share|improve this answer































            1














            Exporting a module in NodeJS turn it singleton, so an option to make it not singleton is to create different instance:



            test.js



            function tesdf(){
            console.log("Gtest test", ind++);
            }

            module.exports = tesdf;


            index.js



            new (require('./test.js'))();





            share|improve this answer































              1














              Try to export function without running, example in your case:



              module.export = testdf


              Before export in this way, import in a variable and run.



              const testdf = require('./test.js')
              testdf()





              share|improve this answer































                1














                test.js



                function tesdf() {
                console.log("Gtest test", ind++);
                }
                module.exports = tesdf;


                index.js



                var test = require('/path/to/test.js');
                test();





                share|improve this answer























                  Your Answer






                  StackExchange.ifUsing("editor", function () {
                  StackExchange.using("externalEditor", function () {
                  StackExchange.using("snippets", function () {
                  StackExchange.snippets.init();
                  });
                  });
                  }, "code-snippets");

                  StackExchange.ready(function() {
                  var channelOptions = {
                  tags: "".split(" "),
                  id: "1"
                  };
                  initTagRenderer("".split(" "), "".split(" "), channelOptions);

                  StackExchange.using("externalEditor", function() {
                  // Have to fire editor after snippets, if snippets enabled
                  if (StackExchange.settings.snippets.snippetsEnabled) {
                  StackExchange.using("snippets", function() {
                  createEditor();
                  });
                  }
                  else {
                  createEditor();
                  }
                  });

                  function createEditor() {
                  StackExchange.prepareEditor({
                  heartbeatType: 'answer',
                  autoActivateHeartbeat: false,
                  convertImagesToLinks: true,
                  noModals: true,
                  showLowRepImageUploadWarning: true,
                  reputationToPostImages: 10,
                  bindNavPrevention: true,
                  postfix: "",
                  imageUploader: {
                  brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                  contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                  allowUrls: true
                  },
                  onDemand: true,
                  discardSelector: ".discard-answer"
                  ,immediatelyShowMarkdownHelp:true
                  });


                  }
                  });














                  draft saved

                  draft discarded


















                  StackExchange.ready(
                  function () {
                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53393114%2fnode-js-why-module-exports-tesdf-from-require-is-executed-only-once-inside%23new-answer', 'question_page');
                  }
                  );

                  Post as a guest















                  Required, but never shown

























                  4 Answers
                  4






                  active

                  oldest

                  votes








                  4 Answers
                  4






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes









                  1














                  When you are exporting a file, you export the function, classes or variables. Here you are exporting the result of tesdf() function. To solve this just export it like:



                  function tesdf(){console.log("Gtest test", ind++); }

                  module.exports = tesdf


                  Also you should not use required('./test.js') inside of the get request because everytime some client request for that end point, the method is going to load the file. e.g



                  let test = require('./test.js');
                  app.get('/', (req, res, next) => {
                  test();//Call the method
                  console.log("test")//after repeated request is executed
                  next();
                  })





                  share|improve this answer




























                    1














                    When you are exporting a file, you export the function, classes or variables. Here you are exporting the result of tesdf() function. To solve this just export it like:



                    function tesdf(){console.log("Gtest test", ind++); }

                    module.exports = tesdf


                    Also you should not use required('./test.js') inside of the get request because everytime some client request for that end point, the method is going to load the file. e.g



                    let test = require('./test.js');
                    app.get('/', (req, res, next) => {
                    test();//Call the method
                    console.log("test")//after repeated request is executed
                    next();
                    })





                    share|improve this answer


























                      1












                      1








                      1







                      When you are exporting a file, you export the function, classes or variables. Here you are exporting the result of tesdf() function. To solve this just export it like:



                      function tesdf(){console.log("Gtest test", ind++); }

                      module.exports = tesdf


                      Also you should not use required('./test.js') inside of the get request because everytime some client request for that end point, the method is going to load the file. e.g



                      let test = require('./test.js');
                      app.get('/', (req, res, next) => {
                      test();//Call the method
                      console.log("test")//after repeated request is executed
                      next();
                      })





                      share|improve this answer













                      When you are exporting a file, you export the function, classes or variables. Here you are exporting the result of tesdf() function. To solve this just export it like:



                      function tesdf(){console.log("Gtest test", ind++); }

                      module.exports = tesdf


                      Also you should not use required('./test.js') inside of the get request because everytime some client request for that end point, the method is going to load the file. e.g



                      let test = require('./test.js');
                      app.get('/', (req, res, next) => {
                      test();//Call the method
                      console.log("test")//after repeated request is executed
                      next();
                      })






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 20 '18 at 12:42









                      SergioEscuderoSergioEscudero

                      1,011516




                      1,011516

























                          1














                          Exporting a module in NodeJS turn it singleton, so an option to make it not singleton is to create different instance:



                          test.js



                          function tesdf(){
                          console.log("Gtest test", ind++);
                          }

                          module.exports = tesdf;


                          index.js



                          new (require('./test.js'))();





                          share|improve this answer




























                            1














                            Exporting a module in NodeJS turn it singleton, so an option to make it not singleton is to create different instance:



                            test.js



                            function tesdf(){
                            console.log("Gtest test", ind++);
                            }

                            module.exports = tesdf;


                            index.js



                            new (require('./test.js'))();





                            share|improve this answer


























                              1












                              1








                              1







                              Exporting a module in NodeJS turn it singleton, so an option to make it not singleton is to create different instance:



                              test.js



                              function tesdf(){
                              console.log("Gtest test", ind++);
                              }

                              module.exports = tesdf;


                              index.js



                              new (require('./test.js'))();





                              share|improve this answer













                              Exporting a module in NodeJS turn it singleton, so an option to make it not singleton is to create different instance:



                              test.js



                              function tesdf(){
                              console.log("Gtest test", ind++);
                              }

                              module.exports = tesdf;


                              index.js



                              new (require('./test.js'))();






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Nov 20 '18 at 12:42









                              Lucas CostaLucas Costa

                              3,58931231




                              3,58931231























                                  1














                                  Try to export function without running, example in your case:



                                  module.export = testdf


                                  Before export in this way, import in a variable and run.



                                  const testdf = require('./test.js')
                                  testdf()





                                  share|improve this answer




























                                    1














                                    Try to export function without running, example in your case:



                                    module.export = testdf


                                    Before export in this way, import in a variable and run.



                                    const testdf = require('./test.js')
                                    testdf()





                                    share|improve this answer


























                                      1












                                      1








                                      1







                                      Try to export function without running, example in your case:



                                      module.export = testdf


                                      Before export in this way, import in a variable and run.



                                      const testdf = require('./test.js')
                                      testdf()





                                      share|improve this answer













                                      Try to export function without running, example in your case:



                                      module.export = testdf


                                      Before export in this way, import in a variable and run.



                                      const testdf = require('./test.js')
                                      testdf()






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Nov 20 '18 at 12:44









                                      Gabriel FurlanGabriel Furlan

                                      91




                                      91























                                          1














                                          test.js



                                          function tesdf() {
                                          console.log("Gtest test", ind++);
                                          }
                                          module.exports = tesdf;


                                          index.js



                                          var test = require('/path/to/test.js');
                                          test();





                                          share|improve this answer




























                                            1














                                            test.js



                                            function tesdf() {
                                            console.log("Gtest test", ind++);
                                            }
                                            module.exports = tesdf;


                                            index.js



                                            var test = require('/path/to/test.js');
                                            test();





                                            share|improve this answer


























                                              1












                                              1








                                              1







                                              test.js



                                              function tesdf() {
                                              console.log("Gtest test", ind++);
                                              }
                                              module.exports = tesdf;


                                              index.js



                                              var test = require('/path/to/test.js');
                                              test();





                                              share|improve this answer













                                              test.js



                                              function tesdf() {
                                              console.log("Gtest test", ind++);
                                              }
                                              module.exports = tesdf;


                                              index.js



                                              var test = require('/path/to/test.js');
                                              test();






                                              share|improve this answer












                                              share|improve this answer



                                              share|improve this answer










                                              answered Nov 20 '18 at 12:57









                                              HOTAM SINGHHOTAM SINGH

                                              61110




                                              61110






























                                                  draft saved

                                                  draft discarded




















































                                                  Thanks for contributing an answer to Stack Overflow!


                                                  • Please be sure to answer the question. Provide details and share your research!

                                                  But avoid



                                                  • Asking for help, clarification, or responding to other answers.

                                                  • Making statements based on opinion; back them up with references or personal experience.


                                                  To learn more, see our tips on writing great answers.




                                                  draft saved


                                                  draft discarded














                                                  StackExchange.ready(
                                                  function () {
                                                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53393114%2fnode-js-why-module-exports-tesdf-from-require-is-executed-only-once-inside%23new-answer', 'question_page');
                                                  }
                                                  );

                                                  Post as a guest















                                                  Required, but never shown





















































                                                  Required, but never shown














                                                  Required, but never shown












                                                  Required, but never shown







                                                  Required, but never shown

































                                                  Required, but never shown














                                                  Required, but never shown












                                                  Required, but never shown







                                                  Required, but never shown







                                                  Popular posts from this blog

                                                  'app-layout' is not a known element: how to share Component with different Modules

                                                  android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

                                                  WPF add header to Image with URL pettitions [duplicate]