annotate Resources/Samples/WebApplications/NodeToolbox.js @ 1224:29cf3dd2cea4

removal of Plustache experiments
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 12 Nov 2014 14:05:02 +0100
parents 009dce4ea2f6
children 6e7e5ed91c2d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
805
56a813a4714d drawing dicomizer sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 /**
56a813a4714d drawing dicomizer sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2 * Orthanc - A Lightweight, RESTful DICOM Store
56a813a4714d drawing dicomizer sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
3 * Copyright (C) 2012-2014 Medical Physics Department, CHU of Liege,
56a813a4714d drawing dicomizer sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4 * Belgium
56a813a4714d drawing dicomizer sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
5 *
56a813a4714d drawing dicomizer sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 * Permission is hereby granted, free of charge, to any person
56a813a4714d drawing dicomizer sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 * obtaining a copy of this software and associated documentation
56a813a4714d drawing dicomizer sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 * files (the "Software"), to deal in the Software without
56a813a4714d drawing dicomizer sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 * restriction, including without limitation the rights to use, copy,
56a813a4714d drawing dicomizer sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 * modify, merge, publish, distribute, sublicense, and/or sell copies
56a813a4714d drawing dicomizer sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 * of the Software, and to permit persons to whom the Software is
56a813a4714d drawing dicomizer sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 * furnished to do so, subject to the following conditions:
56a813a4714d drawing dicomizer sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 *
56a813a4714d drawing dicomizer sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 * The above copyright notice and this permission notice shall be
56a813a4714d drawing dicomizer sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 * included in all copies or substantial portions of the Software.
56a813a4714d drawing dicomizer sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 *
56a813a4714d drawing dicomizer sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
56a813a4714d drawing dicomizer sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
56a813a4714d drawing dicomizer sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
56a813a4714d drawing dicomizer sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
56a813a4714d drawing dicomizer sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
56a813a4714d drawing dicomizer sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
56a813a4714d drawing dicomizer sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
56a813a4714d drawing dicomizer sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 * SOFTWARE.
56a813a4714d drawing dicomizer sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25 **/
56a813a4714d drawing dicomizer sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26
56a813a4714d drawing dicomizer sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27
56a813a4714d drawing dicomizer sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28 var fs = require('fs');
56a813a4714d drawing dicomizer sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29 var http = require('http');
56a813a4714d drawing dicomizer sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30
56a813a4714d drawing dicomizer sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31
56a813a4714d drawing dicomizer sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32 function ForwardGetRequest(orthanc, path, res) {
56a813a4714d drawing dicomizer sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33 var opts = orthanc;
56a813a4714d drawing dicomizer sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34 opts.path = path;
56a813a4714d drawing dicomizer sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35 opts.method = 'GET';
56a813a4714d drawing dicomizer sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36
56a813a4714d drawing dicomizer sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37 http.get(opts, function(response) {
1120
009dce4ea2f6 /tools/create-dicom now accepts PatientID
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 805
diff changeset
38 if (response.statusCode == 200) {
009dce4ea2f6 /tools/create-dicom now accepts PatientID
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 805
diff changeset
39 response.setEncoding('utf-8');
009dce4ea2f6 /tools/create-dicom now accepts PatientID
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 805
diff changeset
40 response.on('data', function(chunk) {
009dce4ea2f6 /tools/create-dicom now accepts PatientID
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 805
diff changeset
41 res.write(chunk);
009dce4ea2f6 /tools/create-dicom now accepts PatientID
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 805
diff changeset
42 });
009dce4ea2f6 /tools/create-dicom now accepts PatientID
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 805
diff changeset
43 response.on('end', function() {
009dce4ea2f6 /tools/create-dicom now accepts PatientID
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 805
diff changeset
44 res.end();
009dce4ea2f6 /tools/create-dicom now accepts PatientID
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 805
diff changeset
45 });
009dce4ea2f6 /tools/create-dicom now accepts PatientID
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 805
diff changeset
46 } else {
009dce4ea2f6 /tools/create-dicom now accepts PatientID
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 805
diff changeset
47 console.log('Got error on GET forwarding: ' +
009dce4ea2f6 /tools/create-dicom now accepts PatientID
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 805
diff changeset
48 response.statusCode + ' (' + path + ')');
009dce4ea2f6 /tools/create-dicom now accepts PatientID
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 805
diff changeset
49 res.writeHead(response.statusCode);
805
56a813a4714d drawing dicomizer sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50 res.end();
1120
009dce4ea2f6 /tools/create-dicom now accepts PatientID
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 805
diff changeset
51 }
805
56a813a4714d drawing dicomizer sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52 }).on('error', function(e) {
1120
009dce4ea2f6 /tools/create-dicom now accepts PatientID
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 805
diff changeset
53 console.log('Unable to contact Orthanc: ' + e.message);
009dce4ea2f6 /tools/create-dicom now accepts PatientID
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 805
diff changeset
54 res.writeHead(503); // Service Unavailable
009dce4ea2f6 /tools/create-dicom now accepts PatientID
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 805
diff changeset
55 res.end();
805
56a813a4714d drawing dicomizer sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56 });
56a813a4714d drawing dicomizer sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57 }
56a813a4714d drawing dicomizer sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58
56a813a4714d drawing dicomizer sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59
56a813a4714d drawing dicomizer sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60 function ForwardPostRequest(orthanc, path, body, res) {
56a813a4714d drawing dicomizer sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61 var opts = orthanc;
56a813a4714d drawing dicomizer sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62 opts.path = path;
56a813a4714d drawing dicomizer sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63 opts.method = 'POST';
56a813a4714d drawing dicomizer sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64 opts.headers = {
56a813a4714d drawing dicomizer sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65 'Content-Length': body.length
56a813a4714d drawing dicomizer sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66 }
56a813a4714d drawing dicomizer sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67
56a813a4714d drawing dicomizer sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68 var req = http.request(opts, function(response) {
1120
009dce4ea2f6 /tools/create-dicom now accepts PatientID
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 805
diff changeset
69 if (response.statusCode == 200) {
009dce4ea2f6 /tools/create-dicom now accepts PatientID
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 805
diff changeset
70 response.setEncoding('utf-8');
009dce4ea2f6 /tools/create-dicom now accepts PatientID
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 805
diff changeset
71 response.on('data', function(chunk) {
009dce4ea2f6 /tools/create-dicom now accepts PatientID
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 805
diff changeset
72 res.write(chunk);
009dce4ea2f6 /tools/create-dicom now accepts PatientID
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 805
diff changeset
73 });
009dce4ea2f6 /tools/create-dicom now accepts PatientID
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 805
diff changeset
74 response.on('end', function() {
009dce4ea2f6 /tools/create-dicom now accepts PatientID
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 805
diff changeset
75 res.end();
009dce4ea2f6 /tools/create-dicom now accepts PatientID
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 805
diff changeset
76 });
009dce4ea2f6 /tools/create-dicom now accepts PatientID
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 805
diff changeset
77 } else {
009dce4ea2f6 /tools/create-dicom now accepts PatientID
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 805
diff changeset
78 console.log('Got error on POST forwarding: ' +
009dce4ea2f6 /tools/create-dicom now accepts PatientID
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 805
diff changeset
79 response.statusCode + ' (' + path + ')');
009dce4ea2f6 /tools/create-dicom now accepts PatientID
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 805
diff changeset
80 res.writeHead(response.statusCode);
805
56a813a4714d drawing dicomizer sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
81 res.end();
1120
009dce4ea2f6 /tools/create-dicom now accepts PatientID
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 805
diff changeset
82 }
805
56a813a4714d drawing dicomizer sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
83 }).on('error', function(e) {
1120
009dce4ea2f6 /tools/create-dicom now accepts PatientID
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 805
diff changeset
84 console.log('Unable to contact Orthanc: ' + e.message);
009dce4ea2f6 /tools/create-dicom now accepts PatientID
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 805
diff changeset
85 res.writeHead(503); // Service Unavailable
009dce4ea2f6 /tools/create-dicom now accepts PatientID
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 805
diff changeset
86 res.end();
805
56a813a4714d drawing dicomizer sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
87 });
56a813a4714d drawing dicomizer sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
88
56a813a4714d drawing dicomizer sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
89 req.write(body);
56a813a4714d drawing dicomizer sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
90 req.end();
56a813a4714d drawing dicomizer sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
91 }
56a813a4714d drawing dicomizer sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
92
56a813a4714d drawing dicomizer sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
93
56a813a4714d drawing dicomizer sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
94 function ServeFile(filename, res) {
56a813a4714d drawing dicomizer sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
95 fs.readFile(filename, function(r, c) {
56a813a4714d drawing dicomizer sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
96 res.end(c.toString());
56a813a4714d drawing dicomizer sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
97 });
56a813a4714d drawing dicomizer sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
98 }
56a813a4714d drawing dicomizer sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
99
56a813a4714d drawing dicomizer sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
100
56a813a4714d drawing dicomizer sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
101 function NotFound(res) {
56a813a4714d drawing dicomizer sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
102 res.writeHead(404, {'Content-Type': 'text/plain'});
56a813a4714d drawing dicomizer sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
103 res.end();
56a813a4714d drawing dicomizer sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
104 }
56a813a4714d drawing dicomizer sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
105
56a813a4714d drawing dicomizer sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
106
56a813a4714d drawing dicomizer sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
107 function Redirect(path, res) {
56a813a4714d drawing dicomizer sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
108 res.writeHead(301, {
56a813a4714d drawing dicomizer sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
109 'Content-Type': 'text/plain',
56a813a4714d drawing dicomizer sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
110 'Location': path
56a813a4714d drawing dicomizer sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
111 });
56a813a4714d drawing dicomizer sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
112 res.end();
56a813a4714d drawing dicomizer sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
113 }
56a813a4714d drawing dicomizer sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
114
56a813a4714d drawing dicomizer sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
115
56a813a4714d drawing dicomizer sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
116 String.prototype.startsWith = function(prefix) {
56a813a4714d drawing dicomizer sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
117 return this.indexOf(prefix) === 0;
56a813a4714d drawing dicomizer sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
118 }
56a813a4714d drawing dicomizer sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
119
56a813a4714d drawing dicomizer sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
120
56a813a4714d drawing dicomizer sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
121 module.exports.ForwardGetRequest = ForwardGetRequest;
56a813a4714d drawing dicomizer sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
122 module.exports.ForwardPostRequest = ForwardPostRequest;
56a813a4714d drawing dicomizer sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
123 module.exports.NotFound = NotFound;
56a813a4714d drawing dicomizer sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
124 module.exports.Redirect = Redirect;
56a813a4714d drawing dicomizer sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
125 module.exports.ServeFile = ServeFile;