forked from ultraeric/hack-intuit-3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestServer.js
101 lines (82 loc) · 2.7 KB
/
testServer.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import express from 'express';
import favicon from 'serve-favicon';
import {MongoClient, ObjectID} from 'mongodb';
import fs from 'fs';
import path from 'path';
import zlib from 'zlib';
import http from 'http';
import * as React from 'react';
import ReactDOMServer from 'react-dom/server';
import {StaticRouter} from 'react-router';
import {addMessengerHooks, sendTextMessage} from './messenger-bot/bot.js'
import addIO from './rest/rest';
const { spawn } = require('child_process');
var port = 9080;
var legacyPort = 8080;
global.window = {
addEventListener: () => {},
scrollTo: () => {}
};
global.document = {
addEventListener: () => {},
createElement: () => {}
};
global.isTest = true;
// var AppComponent = require('./src/App').default;
/* GZIP everything */
function sendBase(req, res, next) {
fs.readFile(__dirname + '/../public/index.html', 'utf8', function (error, docData) {
if (error) throw error;
res.writeHead(200, {'Content-Type': 'text/html', 'Content-Encoding': 'gzip'});
// const AppElement = ReactDOMServer.renderToString(
// <StaticRouter location={req.url} context={{}}>
// <AppComponent/>
// </StaticRouter>
// );
// const document = docData.replace(/<div id="app"><\/div>/,`<div id="app">${AppElement}</div>`);
zlib.gzip(docData, function (_, result) {
res.end(result);
});
});
}
const app = express();
const server = http.createServer(app);
function callback(id, json) {
if (json.type === 'text') {
} else {
sendTextMessage(id, 'Processing your receipt now.');
const python = spawn('python3', ['./receipt_processing/scan.py', json.payload.url]);
python.stdout.on('data', (data) => {
sendTextMessage(id, 'Test');
});
}
}
addMessengerHooks(app, callback);
app.all('*', function(req, res, next){
if (req.path.startsWith('/newuser') || req.path.startsWith('/computers')) {
res.redirect('https://' + req.hostname + ':' + legacyPort + req.path);
return;
}
next();
});
app.use(favicon(path.join(__dirname, '/../public/static/images/logos/favicon.ico')));
// app.get('/bundle.js', function (req, res, next) {
// req.url = req.url + '.gz';
// res.set('Content-Encoding', 'gzip');
// res.set('Content-Type', 'application/javascript');
// next();
// });
//
// app.get('/bundle.css', function (req, res, next) {
// req.url = req.url + '.gz';
// res.set('Content-Encoding', 'gzip');
// res.set('Content-Type', 'text/css');
// next();
// });
app.get('/', sendBase);
app.use(express.static('./public'));
app.get('*', sendBase);
server.listen(port,
() => console.log('Node/express test server started on port ' + port)
);
addIO(server);