comparison OrthancServer/Resources/Samples/Lua/CallWebService.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/Lua/CallWebService.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 /**
23 * This file is a simple echo Web service implemented using
24 * "node.js". Whenever it receives a POST HTTP query, it echoes its
25 * body both to stdout and to the client. Credentials are checked.
26 **/
27
28
29 // Parameters of the ECHO server
30 var port = 8000;
31 var username = 'alice';
32 var password = 'alicePassword';
33
34
35 var http = require('http');
36 var authorization = 'Basic ' + new Buffer(username + ':' + password).toString('base64')
37
38 var server = http.createServer(function(req, response) {
39 // Check the credentials
40 if (req.headers.authorization != authorization)
41 {
42 console.log('Bad credentials, access not allowed');
43 response.writeHead(401);
44 response.end();
45 return;
46 }
47
48 switch (req.method)
49 {
50 case 'POST':
51 {
52 var body = '';
53
54 req.on('data', function (data) {
55 response.write(data);
56 body += data;
57 });
58
59 req.on('end', function () {
60 console.log('Message received: ' + body);
61 response.end();
62 });
63
64 break;
65 }
66
67 default:
68 console.log('Method ' + req.method + ' is not supported by this ECHO Web service');
69 response.writeHead(405, {'Allow': 'POST'});
70 response.end();
71 }
72 });
73
74 server.listen(port);