client/data/simple-persist.js
author ymh <ymh.work@gmail.com>
Thu, 05 Jan 2017 16:26:07 +0100
changeset 647 eaaa1efce396
parent 356 763c925e7a9c
permissions -rw-r--r--
add examples for youtube node types, add a way to open node urls with a single click
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
295
bcac9ea07d04 add simple-persist in grunt connect
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
// Simple persist middle ware
bcac9ea07d04 add simple-persist in grunt connect
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
var path = require('path'),
bcac9ea07d04 add simple-persist in grunt connect
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
    fs = require('fs');
bcac9ea07d04 add simple-persist in grunt connect
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
647
eaaa1efce396 add examples for youtube node types, add a way to open node urls with a single click
ymh <ymh.work@gmail.com>
parents: 356
diff changeset
     5
var jsonFilesBase = path.join(__dirname,"json/example-cinema");
eaaa1efce396 add examples for youtube node types, add a way to open node urls with a single click
ymh <ymh.work@gmail.com>
parents: 356
diff changeset
     6
var resetFilesBase = path.join(__dirname,"example-cinema-src");
eaaa1efce396 add examples for youtube node types, add a way to open node urls with a single click
ymh <ymh.work@gmail.com>
parents: 356
diff changeset
     7
eaaa1efce396 add examples for youtube node types, add a way to open node urls with a single click
ymh <ymh.work@gmail.com>
parents: 356
diff changeset
     8
function getJsonFile(key) {
eaaa1efce396 add examples for youtube node types, add a way to open node urls with a single click
ymh <ymh.work@gmail.com>
parents: 356
diff changeset
     9
  return jsonFilesBase + (key?("-" + key):"") + ".json";  
eaaa1efce396 add examples for youtube node types, add a way to open node urls with a single click
ymh <ymh.work@gmail.com>
parents: 356
diff changeset
    10
}
eaaa1efce396 add examples for youtube node types, add a way to open node urls with a single click
ymh <ymh.work@gmail.com>
parents: 356
diff changeset
    11
eaaa1efce396 add examples for youtube node types, add a way to open node urls with a single click
ymh <ymh.work@gmail.com>
parents: 356
diff changeset
    12
function getResetFile(key) {
eaaa1efce396 add examples for youtube node types, add a way to open node urls with a single click
ymh <ymh.work@gmail.com>
parents: 356
diff changeset
    13
  return resetFilesBase + (key?("-" + key):"") + ".json";
eaaa1efce396 add examples for youtube node types, add a way to open node urls with a single click
ymh <ymh.work@gmail.com>
parents: 356
diff changeset
    14
}
eaaa1efce396 add examples for youtube node types, add a way to open node urls with a single click
ymh <ymh.work@gmail.com>
parents: 356
diff changeset
    15
eaaa1efce396 add examples for youtube node types, add a way to open node urls with a single click
ymh <ymh.work@gmail.com>
parents: 356
diff changeset
    16
function createWriteStreamJson(file) {
eaaa1efce396 add examples for youtube node types, add a way to open node urls with a single click
ymh <ymh.work@gmail.com>
parents: 356
diff changeset
    17
  var dir = path.join(__dirname, 'json');
eaaa1efce396 add examples for youtube node types, add a way to open node urls with a single click
ymh <ymh.work@gmail.com>
parents: 356
diff changeset
    18
  if (!fs.existsSync(dir)){
eaaa1efce396 add examples for youtube node types, add a way to open node urls with a single click
ymh <ymh.work@gmail.com>
parents: 356
diff changeset
    19
    fs.mkdirSync(dir);
eaaa1efce396 add examples for youtube node types, add a way to open node urls with a single click
ymh <ymh.work@gmail.com>
parents: 356
diff changeset
    20
  }
eaaa1efce396 add examples for youtube node types, add a way to open node urls with a single click
ymh <ymh.work@gmail.com>
parents: 356
diff changeset
    21
  return fs.createWriteStream(file);
eaaa1efce396 add examples for youtube node types, add a way to open node urls with a single click
ymh <ymh.work@gmail.com>
parents: 356
diff changeset
    22
}
295
bcac9ea07d04 add simple-persist in grunt connect
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
bcac9ea07d04 add simple-persist in grunt connect
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
exports.middleware = function(req, res, next) {
bcac9ea07d04 add simple-persist in grunt connect
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
    'use strict';
647
eaaa1efce396 add examples for youtube node types, add a way to open node urls with a single click
ymh <ymh.work@gmail.com>
parents: 356
diff changeset
    26
    
eaaa1efce396 add examples for youtube node types, add a way to open node urls with a single click
ymh <ymh.work@gmail.com>
parents: 356
diff changeset
    27
    var params = require('url').parse(req.url, true);
295
bcac9ea07d04 add simple-persist in grunt connect
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
647
eaaa1efce396 add examples for youtube node types, add a way to open node urls with a single click
ymh <ymh.work@gmail.com>
parents: 356
diff changeset
    29
    if (params.pathname !== '/simple-persist') {
295
bcac9ea07d04 add simple-persist in grunt connect
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
      return next();
bcac9ea07d04 add simple-persist in grunt connect
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
    }
bcac9ea07d04 add simple-persist in grunt connect
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
647
eaaa1efce396 add examples for youtube node types, add a way to open node urls with a single click
ymh <ymh.work@gmail.com>
parents: 356
diff changeset
    33
    var key = params.query.key;
eaaa1efce396 add examples for youtube node types, add a way to open node urls with a single click
ymh <ymh.work@gmail.com>
parents: 356
diff changeset
    34
eaaa1efce396 add examples for youtube node types, add a way to open node urls with a single click
ymh <ymh.work@gmail.com>
parents: 356
diff changeset
    35
    var jsonFile = getJsonFile(key);
eaaa1efce396 add examples for youtube node types, add a way to open node urls with a single click
ymh <ymh.work@gmail.com>
parents: 356
diff changeset
    36
    var resetFile = getResetFile(key);
eaaa1efce396 add examples for youtube node types, add a way to open node urls with a single click
ymh <ymh.work@gmail.com>
parents: 356
diff changeset
    37
295
bcac9ea07d04 add simple-persist in grunt connect
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
    res.setHeader('Content-Type', 'application/json; charset=utf-8');
bcac9ea07d04 add simple-persist in grunt connect
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
    res.statusCode = 200;
bcac9ea07d04 add simple-persist in grunt connect
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
355
fa39654e7084 Add Post option to simple persist to be able to simulate "Save Once" which uses Post request
rougeronj
parents: 295
diff changeset
    41
    if(req.method === "PUT" || req.method === "POST") {
647
eaaa1efce396 add examples for youtube node types, add a way to open node urls with a single click
ymh <ymh.work@gmail.com>
parents: 356
diff changeset
    42
      req.pipe(createWriteStreamJson(jsonFile));
356
763c925e7a9c Add 3 second before sending back the result to simulate some latency
rougeronj
parents: 355
diff changeset
    43
      setTimeout(function () {
763c925e7a9c Add 3 second before sending back the result to simulate some latency
rougeronj
parents: 355
diff changeset
    44
    	  res.end(JSON.stringify({result: "OK"}));
763c925e7a9c Add 3 second before sending back the result to simulate some latency
rougeronj
parents: 355
diff changeset
    45
      }, 3000);
295
bcac9ea07d04 add simple-persist in grunt connect
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
    }
bcac9ea07d04 add simple-persist in grunt connect
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
    else {
647
eaaa1efce396 add examples for youtube node types, add a way to open node urls with a single click
ymh <ymh.work@gmail.com>
parents: 356
diff changeset
    48
      var readStream;
eaaa1efce396 add examples for youtube node types, add a way to open node urls with a single click
ymh <ymh.work@gmail.com>
parents: 356
diff changeset
    49
      if('reset' in params.query && params.query.reset) {
295
bcac9ea07d04 add simple-persist in grunt connect
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
        readStream = fs.createReadStream(resetFile);
647
eaaa1efce396 add examples for youtube node types, add a way to open node urls with a single click
ymh <ymh.work@gmail.com>
parents: 356
diff changeset
    51
        readStream.pipe(createWriteStreamJson(jsonFile));
295
bcac9ea07d04 add simple-persist in grunt connect
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
      }
bcac9ea07d04 add simple-persist in grunt connect
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
      else {
bcac9ea07d04 add simple-persist in grunt connect
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
        readStream = fs.createReadStream(jsonFile);
bcac9ea07d04 add simple-persist in grunt connect
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
        readStream.on('error',function() {
bcac9ea07d04 add simple-persist in grunt connect
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
          var srcFile = fs.createReadStream(resetFile);
647
eaaa1efce396 add examples for youtube node types, add a way to open node urls with a single click
ymh <ymh.work@gmail.com>
parents: 356
diff changeset
    57
          srcFile.pipe(createWriteStreamJson(jsonFile));
295
bcac9ea07d04 add simple-persist in grunt connect
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
          srcFile.pipe(res);
bcac9ea07d04 add simple-persist in grunt connect
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
        });
bcac9ea07d04 add simple-persist in grunt connect
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
      }
bcac9ea07d04 add simple-persist in grunt connect
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
      readStream.pipe(res);
bcac9ea07d04 add simple-persist in grunt connect
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
    }
bcac9ea07d04 add simple-persist in grunt connect
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
};