Can I use json file as a module in node.js

时间:2015-05-12 22:44:50

标签: javascript json node.js dynamic-data-exchange

I have a JSON module which contains empty containers like this:

npm config set msvs_version 2012

and I wonder if I can push data into this from another module simply by using { "files": { "rootNeeded":[], "folders":[], "files":[], "images":[], "text":[], "unknown":[] }, } method. Something like ...

array.push

and after this can I use this in a third node module like this...

var myModule=require("./myJsonFile");
function(){
    some magic here...
    myModule.files.files.push(files);
}

in the end it will be like dynamic database each time when program called will be refreshed.

1 个答案:

答案 0 :(得分:1)

You can, however the changes you make will NOT be persisted. Also, if you use cluster, every process will have a different version of your object.

myJsonFile.json

func focusPhoto(recognizer : UITapGestureRecognizer) {
    if photoCaptureDevice.lockForConfiguration(nil) && photoCaptureDevice.focusPointOfInterestSupported {
        photoCaptureDevice.focusPointOfInterest = previewLayer!.captureDevicePointOfInterestForPoint(tapLocation)

        photoCaptureDevice.unlockForConfiguration()
    }
}

mod1.js

{
  "files": {
    "rootNeeded": [],
    "folders": [],
    "files": [],
    "images": [],
    "text": [],
    "unknown": []
  }
}

mod2.js

var json = require('./myJsonFile');

function pushData() {
  json.files.files.push('test 1');
  json.files.files.push('test 2');
}

pushData();