-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode_helper.js
54 lines (47 loc) · 1.53 KB
/
node_helper.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
/*
* File: node_helper.js
* Created: 24/03/2019 by Daniel Burr <[email protected]>
* Description: Node helper for MMM-PageReader
* License: GNU Public License, version 3
*/
const request = require("request")
const bodyParser = require("body-parser")
var NodeHelper = require("node_helper")
module.exports = NodeHelper.create({
start: function() {
console.log(`Starting node helper for: ${this.name}`)
},
socketNotificationReceived: function(notification, payload) {
switch(notification) {
case "PROXY_URL":
this.requestURL(payload)
break
case "LOG":
this.log(payload)
break
}
},
requestURL: function(url) {
request({url: url, method: "GET"}, (error, response, body)=> {
if(error) {
this.log("Cannot open URL: " + url)
this.sendSocketNotification("PROXY", { orig: url })
} else {
this.result = body
this.proxyServe(url)
}
})
},
proxyServe: function(url) {
this.expressApp.use(bodyParser.json())
this.expressApp.use(bodyParser.urlencoded({extended: true}))
this.expressApp.get("/proxied_url", (req, res) => {
var html = this.result
res.status(200).send(html)
})
this.sendSocketNotification("PROXY", { orig: url, proxy: "/proxied_url" })
},
log: function(msg) {
console.log("[MMM-PageReader] " + msg)
},
})