comparison OrthancServer/Resources/Samples/WebApplications/NodeToolbox.js @ 4044:d25f4c0fa160 framework

splitting code into OrthancFramework and OrthancServer
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 10 Jun 2020 20:30:34 +0200
parents Resources/Samples/WebApplications/NodeToolbox.js@94f4a18a79cc
children d9473bd5ed43
comparison
equal deleted inserted replaced
4043:6c6239aec462 4044:d25f4c0fa160
1 /**
2 * Orthanc - A Lightweight, RESTful DICOM Store
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium
6 *
7 * This program is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 **/
20
21
22 var fs = require('fs');
23 var http = require('http');
24
25
26 function ForwardGetRequest(orthanc, path, res) {
27 var opts = orthanc;
28 opts.path = path;
29 opts.method = 'GET';
30
31 http.get(opts, function(response) {
32 if (response.statusCode == 200) {
33 response.setEncoding('utf-8');
34 response.on('data', function(chunk) {
35 res.write(chunk);
36 });
37 response.on('end', function() {
38 res.end();
39 });
40 } else {
41 console.log('Got error on GET forwarding: ' +
42 response.statusCode + ' (' + path + ')');
43 res.writeHead(response.statusCode);
44 res.end();
45 }
46 }).on('error', function(e) {
47 console.log('Unable to contact Orthanc: ' + e.message);
48 res.writeHead(503); // Service Unavailable
49 res.end();
50 });
51 }
52
53
54 function ForwardPostRequest(orthanc, path, body, res) {
55 var opts = orthanc;
56 opts.path = path;
57 opts.method = 'POST';
58 opts.headers = {
59 'Content-Length': body.length
60 }
61
62 var req = http.request(opts, function(response) {
63 if (response.statusCode == 200) {
64 response.setEncoding('utf-8');
65 response.on('data', function(chunk) {
66 res.write(chunk);
67 });
68 response.on('end', function() {
69 res.end();
70 });
71 } else {
72 console.log('Got error on POST forwarding: ' +
73 response.statusCode + ' (' + path + ')');
74 res.writeHead(response.statusCode);
75 res.end();
76 }
77 }).on('error', function(e) {
78 console.log('Unable to contact Orthanc: ' + e.message);
79 res.writeHead(503); // Service Unavailable
80 res.end();
81 });
82
83 req.write(body);
84 req.end();
85 }
86
87
88 function ServeFile(filename, res) {
89 fs.readFile(filename, function(r, c) {
90 res.end(c.toString());
91 });
92 }
93
94
95 function NotFound(res) {
96 res.writeHead(404, {'Content-Type': 'text/plain'});
97 res.end();
98 }
99
100
101 function Redirect(path, res) {
102 res.writeHead(301, {
103 'Content-Type': 'text/plain',
104 'Location': path
105 });
106 res.end();
107 }
108
109
110 String.prototype.startsWith = function(prefix) {
111 return this.indexOf(prefix) === 0;
112 }
113
114
115 module.exports.ForwardGetRequest = ForwardGetRequest;
116 module.exports.ForwardPostRequest = ForwardPostRequest;
117 module.exports.NotFound = NotFound;
118 module.exports.Redirect = Redirect;
119 module.exports.ServeFile = ServeFile;