-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.coffee
50 lines (37 loc) · 1.38 KB
/
app.coffee
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
express = require 'express'
request = require 'request'
app = express()
app.use(express.static __dirname+'/public')
exports.startServer = (port, path, callback) ->
# Request image and convert it to base64
app.get '/encode64/:imgURL', (req, res) ->
if req.params.imgURL
# Original URL has been encoded, we must decode it
imgURL = unescape req.params.imgURL
pattern = /^(\/\/)/
if pattern.test imgURL
imgURL = req.protocol + ':' + imgURL
# Get the distant image
requestParams =
uri: imgURL
encoding: 'binary'
method: "GET"
timeout: 10000
followRedirect: true
maxRedirects: 10
request requestParams, (error, response, body) ->
imageObject =
imageData: null
if not error and response.statusCode is 200
# Image won't be usable if not prefixed by its type and encoding method name
dataUriPrefix = "data:" + response.headers["content-type"] + ";base64,"
image = new Buffer(body.toString(), "binary").toString("base64")
image = dataUriPrefix + image
# We send back encoded image
imageObject.imageData = image
res.send imageObject
# Take any other route and serve it
app.get '*', (req, res) ->
res.sendfile './public/index.html'
app.listen port
console.log 'Express server started on port '+port