-
Notifications
You must be signed in to change notification settings - Fork 0
/
bluebutton_node_listener.js
43 lines (36 loc) · 1.41 KB
/
bluebutton_node_listener.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// Import the required modules
const express = require('express');
const bodyParser = require('body-parser');
// Fron the excellent https://github.com/amida-tech/blue-button project.
const bb = require("@amida-tech/blue-button")
// Create an express app
const app = express();
// Use express middleware for parsing JSON and urlencoded data
app.use(express.json({limit: '50mb'}));
app.use(express.urlencoded({limit: '50mb', extended: true}));
// Use body-parser for parsing JSON and urlencoded data
// Note: As of Express 4.16.0, the body-parser middleware has been built into Express, so explicit usage of body-parser may be unnecessary
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
// Define the route for POST request at /bb
app.post('/bb', (req, res) => {
try {
// Decode the CDA from base64 format and parse it
const cdaxml = Buffer.from(req.body.cda.replace(/[\r\n]+/g, ''), 'base64').toString('utf8');
const cdajson = JSON.stringify(bb.parse(cdaxml))
// Prepare response
let response = {
cda_json: cdajson
}
// Send response with status 200
res.status(200).json(response)
}
catch (error) {
// Send error response with status 400
res.status(400).json(JSON.stringify({error}))
}
});
// Start the server at port 3000
app.listen(3000, () => {
console.log('Server is running on port 3000');
});