-
Notifications
You must be signed in to change notification settings - Fork 2
/
node_helper.js
58 lines (54 loc) · 1.42 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
55
56
57
58
const bodyParser = require("body-parser")
const fs = require('fs')
var path = require('path')
var NodeHelper = require("node_helper")
module.exports = NodeHelper.create({
start: function() {
this.config = null
this.html = null
},
socketNotificationReceived: function(noti, payload) {
var html = (str) => {
return `<html>
<head>
<style>
body {padding:0, margin:0}
</style>
</head>
<body>
${str}
</body>
</html>`
}
if (noti == "INIT") {
this.config = payload
this.html = html(this.config.code)
if (this.config.file) {
var file = path.resolve(__dirname, this.config.file)
if (fs.existsSync(file)) {
fs.readFile(file, (err, data)=>{
if (err) {
console.log("[WIDGET2] File read error:", file)
this.html = this.config.code
} else {
this.html = (this.config.fullHTMLfile) ? data : html(data)
}
})
}
}
this.webserver()
}
},
webserver: function() {
var html = this.html
var uri = `/widget2/` + encodeURI(this.config.uid)
this.expressApp.use(bodyParser.json())
this.expressApp.use(bodyParser.urlencoded({extended: true}))
this.expressApp.get(uri, (req, res) => {
console.log("[WIDGET2] Request:", uri)
res.status(200).send(this.html)
})
console.log("[WIDGET2] Ready for serve:", uri)
this.sendSocketNotification("READY")
}
})