This repository was archived by the owner on Mar 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
97 lines (84 loc) · 3.46 KB
/
Copy pathindex.js
File metadata and controls
97 lines (84 loc) · 3.46 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
84
85
86
87
88
89
90
91
92
93
94
95
96
//Semantics3 UPC Search Engine
var express =require('express');
var bodyParser = require('body-parser');
var credentials = require('./credentials.json');
var api_key = credentials.api_key;
var api_secret = credentials.api_secret;
var sem3 = require('semantics3-node')(api_key,api_secret);
var app = express();
app.use(bodyParser.urlencoded({extended:false}));
//Load the Jade templating engine
app.set('views','./views');
app.set('view engine', 'jade');
app.get('/', function(req, res) {
res.render('home');
});
app.get('/upc', function(req, res) {
res.render('home');
});
app.post('/upc', function(req, res) {
//Error check 1: To ensure upc input was valid numeric input
if(!('upc' in req.body) || isNaN(req.body.upc)) {
res.render('upc', {
title: 'Semantics3 UPC Search Engine - ' + upc,
search: 'UPC: ' + upc,
error: 'You did not enter a valid UPC.'
});
return;
}
var upc = req.body.upc;
// Build the request
sem3.products.products_field( "upc", upc );
// Run the request
sem3.products.get_products(
function(err, products) {
//Error check 2: Internal error while pinging Semantics3 API
if (err ) {
res.render('upc', {
title: 'Semantics3 UPC Search Engine - ' + upc,
search: 'UPC: ' + upc,
error: 'There was an internal server error. Please try again.'
});
}
else {
var productsObj = JSON.parse(products);
//Error check 3: If there was no results because upc could not be found
if(productsObj.results_count == 0) {
res.render('upc', {
title: 'Semantics3 UPC Search Engine - ' + upc,
search: 'UPC: ' + upc,
error: 'No results could be found for the UPC query. Please enter another UPC.'
});
}
else {
//Fields that we are targeting
var fields = ['name','ean','brand','model','category'];
var resObj = {};
for( var index in fields) {
var field = fields[index];
//Set default value as 'N/A'
resObj[field] = 'N/A';
//If field exists in API response
if(field in productsObj.results[0]) {
//Store value
resObj[field] = productsObj.results[0][field];
}
}
//Treat image separately since it's value is an array
resObj['image'] = 'N/A';
//Check if image exists
if(productsObj.results[0].images.length > 0) {
//Save image
resObj['image'] = productsObj.results[0].images[0];
}
res.render('upc', {
title: 'Semantics3 UPC Search Engine - ' + upc,
search: 'UPC: ' + upc,
upc: resObj
});
}
}
}
);
});
app.listen(3019);