-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
95 lines (77 loc) · 2.69 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
// Require and create the Express framework
var express = require('express');
var fileUpload = require('express-fileupload');
var fs = require('fs');
var VisualRecognitionV3 = require('watson-developer-cloud/visual-recognition/v3');
require('dotenv').load();
const API_URL = "https://gateway-a.watsonplatform.net/visual-recognition/api/v3/classify";
const API_KEY = process.env.API_KEY;
const API_VERSION = "2016-05-20";
var COUNTER = 0;
var app = express();
app.use(fileUpload());
// Determine port to listen on
var port = (process.env.PORT || process.env.VCAP_APP_PORT || 3000);
// Enable reverse proxy support in Express. This causes the
// the "X-Forwarded-Proto" header field to be trusted so its
// value can be used to determine the protocol. See
// http://expressjs.com/api#app-settings for more details.
app.enable('trust proxy');
// Add a handler to inspect the req.secure flag (see
// http://expressjs.com/api#req.secure). This allows us
// to know whether the request was via http or https.
if (process.env.NODE_ENV === 'production') {
app.use (function (req, res, next) {
if (req.secure) {
// request was via https, so do no special handling
next();
} else {
// request was via http, so redirect to https
res.redirect('https://' + req.headers.host + req.url);
}
});
}
app.post('/upload', function(req, res) {
if (!req.files)
return res.status(400).send('No files were uploaded.');
// The name of the input field (i.e. "sampleFile") is used to retrieve the uploaded file
var file = req.files.image;
if (file.mimetype !== 'image/jpeg') return res.status(415).send('Wrong filetype')
// Use the mv() method to place the file somewhere on your server
if (COUNTER > 20) COUNTER = 0;
var filePath = `./img/${COUNTER}.jpg`
file.mv(filePath, function(err) {
if (err)
return res.status(500).send(err);
COUNTER++;
var visualRecognition = new VisualRecognitionV3({
version_date: API_VERSION,
api_key: API_KEY,
headers: {
'X-Watson-Learning-Opt-Out': 'true'
}
});
var images_file= fs.createReadStream(filePath);
var params = {
images_file: images_file,
};
visualRecognition.classify(params, function(err, response) {
fs.unlinkSync(filePath);
if (err) {
console.log(err);
res.status(err.code).send('Sorry, Watson has a problum')
}
else {
response = response.images[0].classifiers[0].classes;
res.send(response);
console.log(JSON.stringify(response, null, 2))
};
});
});
});
// Allow static files in /views and /img to be served
app.use(express.static(__dirname + '/views'));
// Start listening on the port
var server = app.listen(port, function() {
console.log('Listening on port %d', server.address().port);
});