javascript - 如何在express.js中的两个javascript文件之间传递参数

标签 javascript node.js express

我正在使用express js,我想将参数从一个javascript文件传递到另一个。我怎样才能实现这个目标?

这两个文件是1.process.js

var WebPageTest = require('webpagetest');
var wpt = new WebPageTest('server address');
var data_url;


exports.process =  function (req, res){

    
    //Running the test
    wpt.runTest(script,{runs: 1},function(err, data) {
        console.log("<----STARTING TEST---->");
        if(err){ console.log(err);}
        data_url = data.data.summaryCSV;
        console.log('-----------');
        console.log(data_url);
        console.log('-----------');
    });
    res.render('index.jade',{par: 'welcome to webpagetest performance, the tests are running in background.'})
};

storedata.js 在这里

var request = require('request');
var CSV = require('csv-string');
var moment = require('moment');
var process = require('./process.js');


exports.storedata = function(req,res){

    var URL;
    var loadTime;
    var TTFB;
    var TTFB1;
    var date;
    var date1;
    var date2;
    var db;
    console.log(process.process.data_url);

    request({uri:process.process.data_url,method:'GET'}, function (error,response,body)  {
                //console.log('----@@@@@@----');
                //console.log(response.headers);
                console.log('----@@@@@@----');
                //console.log(response);
                if (error) {
                    console.log('got an error' + error);
                }
                //console.log(response);
                //console.log(body);
                var data = body;
                console.log('here is the body');
                console.log('----@@@@@@----');
                console.log(body);


                CSV.forEach(data, ',', function (row, index) {
                    if (index == 1 || index == 2) {
                        URL = row[0];
                        loadTime = row[1];

                        TTFB = row[2];
                        TTFB1 = parseInt(TTFB);

                        date = new Date(row[59] * 1000);
                        month = date.getUTCMonth() + 1;
                        month = month.toString();
                        var day = date.getUTCDate();
                        day = day.toString();
                        var year = date.getUTCFullYear();
                        year = year.toString();
                        date = year + "-" + month + "-" + day;
                        date1 = new Date(date);
                        date2 = moment(date1).format('YYYY-MM-DD');

                        //console.log(loadTime);
                        var app_re = new RegExp(/^https\:\/\/some-url/);
                        var staging_re = new RegExp(/^https\:\/\/some-url2/);
                        var webuinqa_re = new RegExp(/^https\:\/\/some-url3/);

                        // Writting into the databse for some-url
                        if(app_re.test(URL)){
                        var db = req.db;
                        var collection = db.get('app');
                        collection.insert({
                            "Date": date2,
                            "TTFB": TTFB1,
                            "loadTime": loadTime,
                            "Url": URL
                        }, function (err, doc) {
                            if (err) {
                                res.send("There was a problem adding the information to the database.");
                            }
                        });}

                        //Writting into the database for some-url2
                        if(staging_re.test(URL)){
                            var db = req.db;
                            var collection = db.get('staging');
                            collection.insert({
                                "Date": date2,
                                "TTFB": TTFB1,
                                "loadTime": loadTime,
                                "Url": URL
                            }, function (err, doc) {
                                if (err) {
                                    res.send("There was a problem adding the information to the database.");
                                }
                            });}

                        //Writting into the database for some-url3
                        if(webuinqa_re.test(URL)){
                            var db = req.db;
                            var collection = db.get('webuinqa');
                            collection.insert({
                                "Date": date2,
                                "TTFB": TTFB1,
                                "loadTime": loadTime,
                                "Url": URL
                            }, function (err, doc) {
                                if (err) {
                                    res.send("There was a problem adding the information to the database.");
                                }
                            });}
                        res.render('index', {title: "All the test Results have been added to the databases, Go to localhost/getData to get the graph"});
                        //res.redirect('/getData');
                    }
                });
    });
};

我想将 process.js 文件中的参数 data_url 传递给 storedata.js 文件。我将在 storedata.js 的方法请求中使用 data_url 的值。

最佳答案

你可以尝试这样的事情:

在您的storedata.js

module.exports = function(data_url) {
  // ...
}

process.js中:

var request = require('request');
var CSV = require('csv-string');
var moment = require('moment');
// The path like this assumes storedata.js and process.js
// are in the same folder.
var storeData = require('./storedata');

exports.process =  function (req, res){
  var URL;
  var loadTime;
  var TTFB;
  var TTFB1;
  var date;
  var date1;
  var date2;
  var db;
  var data_url;

  // initiating the test
  var WebPageTest = require('webpagetest');
  var wpt = new WebPageTest('server-address');

  //Running the test
  wpt.runTest(script,function(err, data) {
      //console.log("hello -->",err || data);

      data_url = data.data.summaryCSV;
      console.log('-----------');
      console.log(data_url);
      console.log('-----------');

      // Once your data_url is ready
      storeData(data_url);

更新:

根据您的评论,这是一个可能的解决方案。

var WebPageTest = require('webpagetest');
var wpt = new WebPageTest('server address');
var data_url;

exports.process =  function (req, res){
    //Running the test
    wpt.runTest(script,{runs: 1},function(err, data) {
        console.log("<----STARTING TEST---->");
        if(err){ console.log(err);}
        data_url = data.data.summaryCSV;
        console.log('-----------');
        console.log(data_url);
        console.log('-----------');
    });
    res.render('index.jade',{par: 'welcome to webpagetest performance, the tests are running in background.'})
};

// Create another method that is able to return the saved data.
exports.getSavedDataURL = function() { return data_url; }

然后在storedata.js

exports.storedata = function(req,res){
    var URL;
    var loadTime;
    var TTFB;
    var TTFB1;
    var date;
    var date1;
    var date2;
    var db;
    // 
    var url = process.getSavedDataURL();

    request({uri:url,method:'GET'}, function (error,response,body)  {

关于javascript - 如何在express.js中的两个javascript文件之间传递参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33730472/

相关文章:

javascript - 如何用node js连接React Js到mysql?

node.js - 如何在 Express for Node 中重定向到单页 Web 应用程序

node.js - NodeJS 中多个 fetch 命令的 Keepalive

javascript - 使用 apollo-datasource-rest 从响应中读取 header

javascript - 如何使用 WAMI 和 impress.js 创建语音控制的幻灯片?

javascript - 在 JQuery 循环中的 Ajax 请求之前发出警报

JavaScript 搜索栏在用户输入后显示结果

node.js - 如何使用curl查询Node.js Express

javascript - 使用 `should` 测试 nodeJS 应用程序时出错(undefined 不是函数)

node.js - Node js中特定数据库操作后使用multer上传文件