This repository has been archived by the owner on Nov 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
80 lines (69 loc) · 3.3 KB
/
server.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
68
69
70
71
72
73
74
75
76
77
78
79
80
const express = require("express");
const path = require("path");
const axios = require("axios");
const fs = require("fs");
const xml2json = require("simple-xml2json");
const server = express();
// Grab the account info from our environment varaibles file and add them to process.env
require("dotenv").config({ path: "variables.env" });
// All HTML files will be served out of a /public folder
server.use(express.static(path.join(__dirname, "./public")));
// If the root directory is browsed, list all the files in the public folder
server.get("/", (req, res) => {
// Grab all the HTML files in the /public folder
// Build an unordered list with links to the files
fs.readdir("./public/", (err, files) => {
const output = files.reduce((prev, curr) => {
return (prev += `<li><a href="/${curr}">${curr}</a></li>`);
}, "");
res.send(`<h2>Files</h2><ul>${output}</ul>`);
});
});
let cookie = ""; // this will store our BREEZESESSION while developing locally
let retries = 0; // The seesion can expire, so we'll try to log in again if that happens. retries will keep track of how many login attempts happen
// All our API request will be hitting /api/xml so we'll catch those request here and proxy them to the Connect server
server.get("/api/xml", async (req, res) => {
// If there isn't a session, login to get a session (using the credentials in variables.env)
if (cookie === "") {
const username = encodeURIComponent(process.env.CONNECT_USERNAME)
const password = encodeURIComponent(process.env.CONNECT_PASSWORD)
const login = await axios({
url: `${process.env.CONNECT_URL}/api/xml?action=login&login=${username}&password=${password}`
});
// Grab the cookie from the login result. We'll pass this with each subsequent request
cookie = login.headers["set-cookie"][0];
}
// Now pass the original request on to the Connect server (like a proxy) and return the result
const results = await axios({
url: `${process.env.CONNECT_URL}${req.url}`,
withCredentials: true,
headers: {
cookie
}
});
// Grab the data from the API result and convert it to JSON
const data = xml2json.parser(results.data);
// There's a chance the session/cookie will expire for lack of activity. If we don't get back a status of "ok", our cookie is probably invalid.
// Clear the cookie and try the whole thing again.
if (data.results.status.code !== "ok" && retries < 2) {
retries++; //increment our retry variable. Only want to rety a couple times so we don't end up in an infinite loop
cookie = "";
console.log(`Cookie is invalid or you are logged out. Retry #${retries}`);
res.redirect(req.url);
} else {
if (retries === 2) {
console.log(
"Node tried 2 times to log in to your Connect account. Check your credentials in varaibles.env. Also try restarting the node server."
);
}
retries = 0; // We can reset retries since we logged in correctly (or failed > 2 times)
// Finally, we send the original XML result back to our client application.
res.send(results.data);
}
});
// Grab the desired development port form the env variables or just use 1999 (e.g. localhost:1999/ )
const port = process.env.PORT || 1999;
// Start the Node development server
server.listen(port, () => {
console.log(`Express is running on port ${port}`);
});