| author | ymh <ymh.work@gmail.com> |
| Thu, 05 Jan 2017 16:26:07 +0100 | |
| changeset 647 | eaaa1efce396 |
| parent 356 | 763c925e7a9c |
| permissions | -rw-r--r-- |
| 295 | 1 |
// Simple persist middle ware |
2 |
var path = require('path'), |
|
3 |
fs = require('fs'); |
|
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 | 23 |
|
24 |
exports.middleware = function(req, res, next) { |
|
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 | 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 | 30 |
return next(); |
31 |
} |
|
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 | 38 |
res.setHeader('Content-Type', 'application/json; charset=utf-8'); |
39 |
res.statusCode = 200; |
|
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 | 46 |
} |
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 | 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 | 52 |
} |
53 |
else { |
|
54 |
readStream = fs.createReadStream(jsonFile); |
|
55 |
readStream.on('error',function() { |
|
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 | 58 |
srcFile.pipe(res); |
59 |
}); |
|
60 |
} |
|
61 |
readStream.pipe(res); |
|
62 |
} |
|
63 |
}; |