我在节点上使用快速框架,我想在每次运行模块时更改测试模块中变量的值(增加或减少)。有没有办法知道文件是否已被执行并将其保存在内存中,以便下次再次运行文件时,值会再次更改?我想在每次执行模块时增加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);
})();
}
答案 0 :(得分:1)
一种解决方案是在cookie会话中存储您需要的信息:
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
});
但这是为了存储连接到服务器的浏览器的信息。
其他解决方案是通过将所需的值与其他相关信息附加到其中来维护日志文件。
答案 1 :(得分:0)
检查Express API参考中的app.locals:http://expressjs.com/api.html#app.locals