我在 Node 上使用快速框架,我想在每次运行模块时更改测试模块中变量的值(增加或减少)。有没有办法知道文件是否已经执行并将其保存在内存中,以便下次再次运行文件时,值再次发生变化?我想在每次执行模块时增加 variableToChange
。
这是我的代码:
'use strict';
var util = require('util');
var makeApiCall = require('proc-utils').makeApiCall;
var baseUrl = 'http://localhost:' + (require('../../config').port || 3000);
var url = {
endpoint: '/patients/register',
method: 'post'
};
var ct = {
test: function (data, cb) {
var options = {
type: 'form',
data: data,
baseUrl: baseUrl
};
makeApiCall(url.endpoint, url.method, options, cb);
}
};
module.exports = ct;
//-- Test Code ----------------------------------------------------------
var variableToChange=0;
if (require.main === module) {
(function () {
var data = {
first_name:'John',
last_name:'Doe',
email:'shnsdfn'+variableToChange+'b@sh.com',
password:'John1234'
};
ct.test(data, console.log);
})();
}
最佳答案
一种解决方案是将您需要的信息存储在 cookie session 中:
var cookieParser = require('cookie-parser');
var session = require('express-session');
express().use(cookieParser()) // include the cookies middleware to parse and manage sessions
.use(session({secret : 'mysecret'}))//set up the session with minimal config (secret)
.use(function(req,res,next){
req.session.variableToSave = 0; // initialize variable you want to save
next(); // starts next middleware
});
但这是为了存储连接到服务器的浏览器的信息。
其他解决方案是通过向日志文件附加所需的值和其他相关信息来维护日志文件。
关于javascript - 我可以知道一个javascript文件是否已经被执行了吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26749175/