表达来自json stranges的配置

时间:2017-11-20 14:54:43

标签: node.js express routes

所有人的好时光。最近开始学习javascript和实验节点服务器编码,现在真的无法理解发生了什么..

我有经典的app.js

...
var view = require('./routes/view');
...
app.use('/mix', view);
...

这是我的观点.js

var express = require('express');
var router = express.Router();
var moment = require("moment");

var DB = require('../modules/mysql')

/*  */
router.get('/:mixId', (req, res, next) => {

  /** set defaults */
  let config = require('../cfg/hbs.json'); // config load (see example below)
  var page = "pages/viewFail",
    alert = {};

  /** select info about mix */
  DB.query('SELECT startTS, targetAdr, received FROM mixes m WHERE name=?', [req.params.mixId], (err, results) => {

    /** if not exist */
    if (results.length === 0) {
      alert = { // danger alert
        "color": "danger",
        "text": "this mix does not exist"
      }
    }
    /** exist */
    else {

      var mix = {
        "adr": results[0].targetAdr,
        "received": results[0].received,
        /** calculate endin in minudes */
        "endin": parseInt(120 - moment.duration(moment().diff(moment(results[0].startTS))).asMinutes()),
      }

      /** check end time and ballance. if no transactions and time ends - exit */
      if (mix.endin <= 0 && mix.received <= 0) {
        alert = {
          "color": "danger",
          "text": "this mix does not exist"
        }
      } else {

        if (mix.received == 0) {
          page = 'pages/viewStart'
        } else {
          config.meta.js.push("/js/viewEnd.js"); /** add common page js {} */
          page = 'pages/viewEnd'

          console.log(config.meta)
          console.log("\n------------\n")

        }
      }
    }


    /** send response */
    res.render(page, {
      layout: 'layout',
      meta: config.meta,
      alert: alert,
      mix: mix,
    });
  });
});

module.exports = router;

这是我的配置文件:

{
  "meta": {
    "title": "test partials",
    "css": [
      "https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css",
      "/css/c.css"
    ],
    "js": [
      "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js",
      "https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.3/umd/popper.min.js",
      "https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js"
    ],
    "tmp": [
      "https://code.jquery.com/jquery-3.2.1.slim.min.js"
    ]

  }
}

问题是:当我重新启动页面时,meta.js会一次又一次地添加,结果是:

{ title: 'test partials',
  css: 
   [ 'https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css',
     '/css/c.css' ],
  js: 
   [ 'https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js',
     'https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.3/umd/popper.min.js',
     'https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js',
     '/js/viewEnd.js',  // ??????
     '/js/viewEnd.js',  // ??????
     '/js/viewEnd.js' ],// ??????
  tmp: [ 'https://code.jquery.com/jquery-3.2.1.slim.min.js' ] }

那么我做错了什么?

1 个答案:

答案 0 :(得分:1)

请考虑以下事项:

let config1 = require('../cfg/hbs.json');
let config2 = require('../cfg/hbs.json');

console.log(config1 === config2);

这将记录值true,这意味着config1config2指的是完全相同的对象。

原因是require执行缓存。它只会在第一次读取文件。对require相同文件的后续调用将只返回缓存对象。

这就是你的代码中发生的事情。每次调用路由时,它都会拉入相同的config对象,然后将一个额外的条目推入同一个数组中。

有几种方法可以解决这个问题。一个简单的解决方案是通过JSON编码来获取对象的深层副本:

let config = JSON.parse(JSON.stringify(require('../cfg/hbs.json')));