Node.js 节点js:为什么require中的module.exports=tesdf()只在get请求中执行一次

Node.js 节点js:为什么require中的module.exports=tesdf()只在get请求中执行一次,node.js,get,require,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 execut

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()
如何使函数再次调用。
谢谢。

导出文件时,需要导出函数、类或变量。这里是导出
tesdf()
函数的结果。要解决此问题,只需将其导出为:

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

module.exports = tesdf
另外,您不应该在get请求中使用
required('./test.js')
,因为每次客户端请求该端点时,该方法都将加载该文件。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();
    })

导出文件时,导出函数、类或变量。这里是导出
tesdf()
函数的结果。要解决此问题,只需将其导出为:

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

module.exports = tesdf
另外,您不应该在get请求中使用
required('./test.js')
,因为每次客户端请求该端点时,该方法都将加载该文件。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();
    })

在NodeJS中导出模块将其转换为singleton,因此使其不是singleton的一个选项是创建不同的实例:


test.js

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

module.exports = tesdf;
new (require('./test.js'))();
function tesdf() {
    console.log("Gtest test", ind++); 
}
module.exports = tesdf;
var test = require('/path/to/test.js');
test();
index.js

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

module.exports = tesdf;
new (require('./test.js'))();
function tesdf() {
    console.log("Gtest test", ind++); 
}
module.exports = tesdf;
var test = require('/path/to/test.js');
test();

在NodeJS中导出模块将其转换为singleton,因此使其不是singleton的一个选项是创建不同的实例:


test.js

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

module.exports = tesdf;
new (require('./test.js'))();
function tesdf() {
    console.log("Gtest test", ind++); 
}
module.exports = tesdf;
var test = require('/path/to/test.js');
test();
index.js

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

module.exports = tesdf;
new (require('./test.js'))();
function tesdf() {
    console.log("Gtest test", ind++); 
}
module.exports = tesdf;
var test = require('/path/to/test.js');
test();

尝试在不运行的情况下导出函数,例如:

module.export = testdf
在以这种方式导出之前,请导入变量并运行

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

尝试在不运行的情况下导出函数,例如:

module.export = testdf
在以这种方式导出之前,请导入变量并运行

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

test.js

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

module.exports = tesdf;
new (require('./test.js'))();
function tesdf() {
    console.log("Gtest test", ind++); 
}
module.exports = tesdf;
var test = require('/path/to/test.js');
test();
index.js

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

module.exports = tesdf;
new (require('./test.js'))();
function tesdf() {
    console.log("Gtest test", ind++); 
}
module.exports = tesdf;
var test = require('/path/to/test.js');
test();

test.js

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

module.exports = tesdf;
new (require('./test.js'))();
function tesdf() {
    console.log("Gtest test", ind++); 
}
module.exports = tesdf;
var test = require('/path/to/test.js');
test();
index.js

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

module.exports = tesdf;
new (require('./test.js'))();
function tesdf() {
    console.log("Gtest test", ind++); 
}
module.exports = tesdf;
var test = require('/path/to/test.js');
test();