Skip to content

Commit

Permalink
Follow redirects (#3)
Browse files Browse the repository at this point in the history
* Attempt a cleaner error handling
  • Loading branch information
Matteo Giordano authored and Giovanni Toraldo committed Mar 2, 2017
1 parent fcd38fa commit a99b792
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 11 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
"description": "Extract prominent colors from an image",
"main": "src/server.js",
"dependencies": {
"cors": "^2.8.1",
"express": "^4.14.1",
"node-vibrant": "^2.1.2",
"cors": "^2.8.1"
"request": "^2.79.0"
},
"devDependencies": {
"should": "^11.2.0",
Expand Down
39 changes: 30 additions & 9 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,52 @@ const http = require('http');
const express = require('express');
const cors = require('cors');
const Vibrant = require('node-vibrant');
const request = require('request');

const app = express();
const server = http.createServer(app);

app.use(cors());

app.get('/', (req, res) => {
app.get('/', (req, res, next) => {
const imgUrl = req.query.imgUrl;

if (imgUrl == undefined || imgUrl == '') {
res.status(400).send('Missing `imgUrl` parameter');
return;
let err = new Error('missing `imgUrl` parameter');
err.status = 400;
throw err;
}

Vibrant.from(imgUrl).getPalette((err, palette) => {
if (err != null) {
res.status(500).send(err.toString());
request({
url: imgUrl,
encoding: null
}, (error, response, body) => {
if (error != null) {
res.status(500).send(error);
return next(error);
}

const maxAge = process.env.MAX_AGE || 1296000; // 15 days
res.set('Cache-Control', 'public, max-age=' + maxAge);
res.json(palette);
Vibrant.from(body).getPalette((err, palette) => {
if (err != null) {
res.status(500).send(err.toString());
return next(err);
}

const maxAge = process.env.MAX_AGE || 1296000; // 15 days
res.set('Cache-Control', 'public, max-age=' + maxAge);
res.json(palette);
});
});
});

app.use(function(err, req, res, next) {
if (err.status !== 400) {
return next();
}

res.status(400).send(err.toString());
});

server.listen(process.env.PORT || 3000, process.env.IP || '0.0.0.0', () => {
const addr = server.address();
console.log('Vivace listening at', addr.address + ':' + addr.port);
Expand Down
20 changes: 19 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ describe('loading express', () => {
});
});

it('follows redirects', (done) => {
request(server)
.get('/?imgUrl=https://backend.cloudesire.com/api/productImageFile/static/883.png')
.expect(200)
.end((err, res) => {
res.body.should.have.property('LightMuted');
res.body.LightMuted.should.have.property('rgb');
res.body.LightMuted.rgb.should.containDeep([153, 170, 171]);
done();
});
});

it('responds to preflight cors requests', (done) => {
request(server)
.options('/?imgUrl=https://appshop.cloud/images/marketplace/logo.png')
Expand All @@ -36,7 +48,7 @@ describe('loading express', () => {
});
});

const error = 'Missing `imgUrl` parameter';
const error = 'Error: missing `imgUrl` parameter';

it('imgUrl parameter is required', (done) => {
request(server).get('/').expect(400, error, done);
Expand All @@ -51,4 +63,10 @@ describe('loading express', () => {
.get('/foo/bar')
.expect(404, done);
});

it('500 when not 200 or 300', (done) => {
request(server)
.get('/?imgUrl=https://httpbin.org/status/418')
.expect(500, done);
}).timeout(5000);
});

0 comments on commit a99b792

Please sign in to comment.