-
Notifications
You must be signed in to change notification settings - Fork 1
/
gateway.js
67 lines (59 loc) · 1.84 KB
/
gateway.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
require("dotenv").config();
const mllp = require("./index.js");
const express = require("express");
const httpServer = express();
httpServer.listen(process.env.HTTP_PORT, () => {
console.log(`HTTP server listening on port ${process.env.HTTP_PORT}`);
});
httpServer.get("/", (req, res) => {
res.json({
status: `HTTP server listening for POST on port ${process.env.HTTP_PORT}`,
});
});
var mllpServer = new mllp.MLLPServer(
process.env.MLLP_HOST,
process.env.MLLP_PORT
);
let er7Msg = null;
httpServer.use(express.text());
httpServer.post("/", (req, res) => {
console.log("\n\n\n== Gateway: HTTP POST received");
if (!req.headers["forward-to"] || !req.headers["forward-to"].startsWith("mllp://")) {
console.log("- ERROR: Forward-To header not found, returning 400");
res.json({
error:"Forward-To must be set in Header and begin with 'mllp://'"
}).status(400);
return
}
const forwardToSplit = req.headers["forward-to"].split(":");
const remoteMllpHost = forwardToSplit[1].substring(2);
const remoteMllpPort = parseInt(forwardToSplit[2]);
console.log(
`- Found Forward-To header - host: ${remoteMllpHost} | port: ${remoteMllpPort}`
);
er7Msg = req.body;
console.log(
`- Forwarding request body to mllp://${remoteMllpHost}:${remoteMllpPort}\n`
);
console.log(er7Msg);
mllpServer.send(
remoteMllpHost,
remoteMllpPort,
er7Msg,
function (err, ackData) {
console.log("- MLLP response error: " + err);
console.log("- MLLP response ackData:");
console.log(ackData);
const payload = {};
let status = 200;
if(err){
payload.error = err;
status = 500
}
if(ackData) payload.er7 = ackData;
console.log("\nResponding to HTTP request:");
console.log(JSON.stringify(payload));
res.json(payload).status(status);
}
);
});