forked from node-red/node-red
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple-server.js
69 lines (61 loc) · 1.71 KB
/
simple-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
var express = require('express');
var request = require('request');
var requestProxy = require('express-request-proxy');
var fs = require('fs');
var async = require('async');
var app;
var server;
module.exports = {
init: function (callback) {
app = express();
app.use(express.static('build/public'));
var nodes = {
json: undefined,
html: undefined
};
app.get('/nodes', function (req, res) {
if (req.get("accept") == "application/json") {
if (!nodes.json) {
nodes.json = fs.readFileSync('static-api/nodes.json');
}
res.contentType('application/json; charset=utf-8');
res.send(nodes.json);
} else {
if (!nodes.html) {
nodes.html = fs.readFileSync('static-api/nodes.html');
}
res.send(nodes.html);
}
});
app.all('/*', function (req, res, next) {
next();
}, requestProxy({
url: "http://localhost:1880/*"
}));
if (callback) {
callback();
}
},
start: function (callback) {
if (!server) {
server = app.listen(8081, function () {
console.log('Example app listening on port ' + server.address().port + '!');
if (callback) {
callback();
}
});
}
},
stop: function (callback) {
if (server) {
server.close();
server = undefined;
}
if (app) {
app = undefined;
}
if (callback) {
callback();
}
}
};