-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathserver.js
More file actions
83 lines (69 loc) · 2.61 KB
/
server.js
File metadata and controls
83 lines (69 loc) · 2.61 KB
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
81
82
83
const express = require('express');
var proxy = require('http-proxy-middleware');
var bodyParse = require('body-parser');
var exec = require('child_process').exec;
let port = 3000;
let server = express();
// Settings
server.set('port', process.env.PORT || port);
exec('php -S localhost:9000');
server.use(bodyParse.json());
server.use(bodyParse.urlencoded({extended: true}));
// Log all api requests
var endPoint = '';
server.all('/api/:reqType', function(req, res, next){
endPoint = req.params.reqType;
console.log('Api request:: ', endPoint);
next();
});
// Middlewares
server.use('/assets', express.static(require('path').join(__dirname, 'assets')));
server.use('/docs', express.static(require('path').join(__dirname, 'docs')));
server.use('/content', express.static(require('path').join(__dirname, 'admin/content')));
server.use('/admin/assets', express.static(require('path').join(__dirname, 'admin/assets')));
// server.use('/api', proxy({target: 'http://0.0.0.0:9000/api/wpos.php', changeOrigin: true}));
// server.use('/socket.io', proxy({target: 'http://35.224.196.185/', changeOrigin: true}));
server.all('/api/:reqType', function(req, res, next){
if (req.body) {
let bodyData = JSON.stringify(req.body);
// incase if content-type is application/x-www-form-urlencoded -> we need to change to application/json
proxy.setHeader('Content-Type','application/json');
proxy.setHeader('Content-Length', Buffer.byteLength(bodyData));
// stream the content
proxy.write(bodyData);
}
var endPoint = 'http://0.0.0.0:9000/api/wpos.php?a='+req.params.reqType;
console.log(endPoint);
server.use('/api', proxy({target: endPoint, changeOrigin: true}));
res.end();
});
function onProxyReq(proxyReq, req, res) {
if (req.body) {
let bodyData = JSON.stringify(req.body);
// incase if content-type is application/x-www-form-urlencoded -> we need to change to application/json
proxyReq.setHeader('Content-Type','application/json');
proxyReq.setHeader('Content-Length', Buffer.byteLength(bodyData));
// stream the content
proxyReq.write(bodyData);
} else {
console.log('no body..')
}
}
function onProxyRes(proxyReq, req, res) {
console.log('Data returned');
console.log(res);
};
// Routes
server.get('/', function(req, res, next){
res.sendFile(__dirname + '/index.html');
});
server.get('/wpos.appcache', (req, res, next)=>{
res.sendFile(__dirname + '/wpos.appcache');
});
server.get('/admin', (req, res, next)=>{
res.sendFile(__dirname + '/admin/index.html');
});
// Start server
server.listen(server.get('port'), ()=>{
console.log('Server is running...');
});