LoginSignup
4
2

More than 5 years have passed since last update.

ORCAにnode.jsでアクセスしてみた꒰。・ω・`;꒱

Last updated at Posted at 2017-05-12

チェック方法

  • expressから、ORCAにHTTPで患者情報を取得し、その結果を表示する

ファイル構成

console
+ express_app
  + index.js
  + htdocs/
    + index.html
    + app.js
+ node_modules
+ package.json

起動方法

console
$ node index.js

パッケージの追加

console
$ npm install --save xml2js

患者情報取得

クライアントサイド

index.js
<!DOCTYPE html>
<html lang="ja">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <script
    src="http://code.jquery.com/jquery-3.2.1.min.js"
    integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
    crossorigin="anonymous"></script>
  <script type="text/javascript" src="app.js"></script>
</head>
<body>
  <h1>index.html</h1>
  <p id="result"></p>
  <button id="loadPatient">読込</button>
  <!--button id="loadPatient_Post">読込(POST)</button-->
</body>
</html>
app.js
$(function() {
  $('#loadPatient').on('click', function() {
    $.ajax({
      url: '/',
      type: 'POST',
      dataType: 'json',
      data: { patient_id: 1 },
      timeout: 3000,
      success: function(json) {
        if (json.data) {
          $('#result').text(json.data.name);
        } else {
          $('#result').text('error');
        }
      },
      error: function(XMLHttpRequest, textStatus, errorThrown) {
        alert('患者データを読み込めませんでした');
        $('#result').text('error');
      }
    });
  });
});

サーバサイド

index.js
var express = require('express');

var app = express();

// POSTパラメータ受信用
app.use(bodyParser.urlencoded({
    extended: true
}));
app.use(bodyParser.json());

// XML->JSON変換用
var parseString = require('xml2js').parseString;

// htdocsに静的なページを用意する
app.use(express.static('htdocs'));

app.set('port', 5000);

// POST '/' 
// PARAM patient_id 患者番号
app.post('/', function(req, res) {
  // リクエストボディを取得する
  console.log(req.body);
  const patient_id = req.body.patient_id;
  if (patient_id) {
    const username = 'ormaster';
    const pswd = 'ormaster';
    var options = {
      host: '[IP]',
      port: 8000,
      path: '/api01rv2/patientgetv2?id=' + patient_id,
      headers: {
        'Authorization': 'Basic ' + new Buffer(username + ':' + pswd).toString('base64')
      }
    };

    var http = require('http');
    var data = null;
    http.get(options, function(res_) {
      res_.on('data', function(chunk) {
        data += chunk.toString();
      }).on('end', function() {
        var xml = '<root>' + data + '</root>';
        parseString(xml, function(err, result) {
          if (result) {
            const patient_name =
              result.root.xmlio2[0].patientinfores[0].Patient_Information[0].WholeName[0]._;
            res.json({ 'data': { 'id': patient_id, 'name': patient_name } });
          }
        });
      });
    }).on("error", function(e) {
      res.json({ 'err': { 'id': patient_id, name: '' } });
      console.log("Got error: " + e.message);
    });
  }
})

app.listen(app.get('port'), function() {
  console.log("Node app is running at localhost:" + app.get('port'))
});
4
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
2