node js: Why module.exports = tesdf() from require is executed only once, inside get request
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
add a comment |
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
add a comment |
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
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
node.js get require
asked Nov 20 '18 at 12:34
TflagTflag
184
184
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
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();
})
add a comment |
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'))();
add a comment |
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()
add a comment |
test.js
function tesdf() {
console.log("Gtest test", ind++);
}
module.exports = tesdf;
index.js
var test = require('/path/to/test.js');
test();
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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();
})
add a comment |
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();
})
add a comment |
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();
})
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();
})
answered Nov 20 '18 at 12:42
SergioEscuderoSergioEscudero
1,011516
1,011516
add a comment |
add a comment |
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'))();
add a comment |
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'))();
add a comment |
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'))();
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'))();
answered Nov 20 '18 at 12:42
Lucas CostaLucas Costa
3,58931231
3,58931231
add a comment |
add a comment |
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()
add a comment |
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()
add a comment |
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()
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()
answered Nov 20 '18 at 12:44
Gabriel FurlanGabriel Furlan
91
91
add a comment |
add a comment |
test.js
function tesdf() {
console.log("Gtest test", ind++);
}
module.exports = tesdf;
index.js
var test = require('/path/to/test.js');
test();
add a comment |
test.js
function tesdf() {
console.log("Gtest test", ind++);
}
module.exports = tesdf;
index.js
var test = require('/path/to/test.js');
test();
add a comment |
test.js
function tesdf() {
console.log("Gtest test", ind++);
}
module.exports = tesdf;
index.js
var test = require('/path/to/test.js');
test();
test.js
function tesdf() {
console.log("Gtest test", ind++);
}
module.exports = tesdf;
index.js
var test = require('/path/to/test.js');
test();
answered Nov 20 '18 at 12:57
HOTAM SINGHHOTAM SINGH
61110
61110
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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