forked from derekoneil/alpha-office-product-catalog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
138 lines (114 loc) · 3.95 KB
/
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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
'use strict';
var http = require('http');
var express = require('express');
var request = require('request');
var path = require('path');
var bodyParser = require('body-parser')
var fs = require('fs')
var spawn = require('child_process')
var fileUpload = require('express-fileupload');
var hostString = process.argv[2]
var PORT = process.env.PORT || 8085;
var app = express();
var myJSON = JSON.parse(fs.readFileSync('product-catalog.json'));
app.use(bodyParser.json())
app.use(fileUpload());
// app.use(express.static(__dirname))
app.use(express.static('./public'));
app.get('/', function (req, res) {
res.sendFile(path.join(__dirname + '/public/alpha.html'));
});
app.get('/statictweets/:search', function (req, res) {
console.log('Calling twitter feed service at: ' + 'http://twitter-feed:30000/statictweets/' + encodeURIComponent(req.params.search));
request('http://twitter-feed:30000/statictweets/' + encodeURIComponent(req.params.search), function (error, response, body) {
console.log('error:', error);
console.log('statusCode:', response && response.statusCode);
console.log('body:', body);
res.end(body);
});
});
app.get('/color', function(req, res) {
console.log('getting color from twitter feed');
request('http://twitter-feed:30000/statictweets/color', function (error, response, body) {
console.log('error:', error);
console.log('statusCode:', response && response.statusCode);
console.log('body:', body);
res.end(body);
});
});
app.post('/upload', function(req, res) {
if (!req.files)
return res.status(400).send('No files were uploaded.');
console.log('processing file upload');
// The name of the input field (i.e. "sampleFile") is used to retrieve the uploaded file
let sampleFile = req.files.sampleFile;
const options = {
method: 'POST',
uri: 'http://my-release-fn-api/r/imgconvert/resize128',
encoding: null,
headers: {
'Content-type': 'application/octet-stream'
},
body: sampleFile.data
};
request(options, function(error, response, body) {
console.log('processing image thumbnail');
console.log('error:', error);
console.log('statusCode:', response && response.statusCode);
console.log('body:', body);
if (response && response.statusCode == 200) {
console.log('Status Code: ' + response.statusCode);
fs.writeFile('public/thumbnail.png', body, 'binary', function(err){
if (err) throw err
console.log('File saved.')
var options = {
root: __dirname + '/public/',
dotfiles: 'deny',
headers: {
'x-timestamp': Date.now(),
'x-sent': true
}
};
// res.sendFile('thumbnail.png', options);
res.send(body.toString('base64'));
});
}
else {
res.status(500);
res.end();
}
});
// setTimeout(() => res.redirect('/'), 1000);
});
/*
* route /products has been replaced in this example with static JSON data
*/
app.get('/products', function(req, res) {
console.log('/products api called')
res.end(JSON.stringify(myJSON));
});
app.get('/searchProductNames', function(req, res) {
console.log('/productNames api called')
res.end(JSON.stringify(
myJSON.Products
.filter(product => product.PRODUCT_NAME.includes(req.query.term))
.map(
product => (({PRODUCT_NAME, PRODUCT_ID}) => ({PRODUCT_NAME, PRODUCT_ID}))(product)
)));
});
app.get('/productNames', function(req, res) {
console.log('/productNames api called');
res.end(JSON.stringify(
myJSON.Products.map(
product => (({PRODUCT_NAME, PRODUCT_ID}) => ({PRODUCT_NAME, PRODUCT_ID}))(product)
)));
});
app.get('/product/:id', function(req, res) {
console.log('/product api called')
res.end(JSON.stringify(
myJSON.Products.filter(product => product.PRODUCT_ID == req.params.id)
));
});
app.listen(PORT, function() {
console.log('AlphaOffice listening on port ' + PORT);
});