-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
95 lines (70 loc) · 2.44 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
var express = require('express');
var app = express();
var router = express.Router();
var dotenv = require('dotenv').config();
var morgan = require('body-parser');
var bodyParser = require('body-parser');
var methodOverride = require('method-override');
var multer = require('multer');
//node modules
app.use(morgan('dev'));
app.use(bodyParser.urlencoded({'extended': 'true'}))
app.use(bodyParser.json());
app.use(bodyParser.json({type: 'application/vnd.api+json'}))
app.use(methodOverride());
app.use(express.static(__dirname + '/public')); //sending all files to the front
//watson
var watson = require('watson-developer-cloud');
var fs = require('fs');
var visual_recognition = watson.visual_recognition({
api_key: process.env.API_KEY,
version: 'v3',
version_date: '2016-05-19'
});
//
//routes
//create your route
//access the data from the request body
// console.log the data
//send back a different piece of data (create another object literal and jsonify it)
app.post('/endpoint', function(req, res){
// console.log('body: ' + JSON.stringify(req.body));
//call watson logic here
var params = {
images_file: fs.createReadStream('./public/images/' + req.body.image)
};
visual_recognition.classify(params, function(err, response) {
if (err)
console.log(err);
else
console.log(response)
var success = JSON.stringify(response, null, 2);
res.send(success);
});
});
//uploading images
var upload = multer({
dest: __dirname + '../public/images',
})
app.post('/upload', upload.single('image'), function(req,res){
console.log(req.file.destination)
/** When using the "single"
data come in "req.file" regardless of the attribute "name". **/
var tmp_path = req.file.path;
/** The original name of the uploaded file
stored in the variable "originalname". **/
var target_path = './public/images/' + req.file.originalname;
var json_image_name = req.file.originalname
/** A better way to copy the uploaded file. **/
var src = fs.createReadStream(tmp_path);
var dest = fs.createWriteStream(target_path);
src.pipe(dest);
src.on('end', function() { console.log('complete'); res.send(json_image_name) });
src.on('error', function(err) { console.log('error'); });
})
//second iteration
// send that data through to watson params and visual recognition
// if success through watson, send that data in response body back to the front
//listen
app.listen(8080);
console.log("App listening on port 8080")