aboutsummaryrefslogtreecommitdiff
path: root/examples/scalajs-react-example/server/app.js
blob: d44adcc9dc4ca07cee5f7104f6fe8b93ed3b7dd3 (plain) (blame)
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
var express = require('express');
var https = require('https');

var app = express();

app.get('/data', function(req, res){

  var request = https.get(
    //'https://api.instagram.com/v1/media/popular?client_id=642176ece1e7445e99244cec26f4de1f&callback=?',
    'https://pixabay.com/api/?key=2741116-9706ac6d4a58f2b5416225505&q=yellow+flowers&image_type=photo',
    function(response) {
      var body = "";
      response.on('data', function(data) {
        body += data;
      });
      response.on('end', function() {
        console.log(body);
        try {
          res.send(JSON.parse(body));
        } catch (e) {
          return console.error(e);
        }
      });
    }
  );
  request.on('error', function(e) {
    console.log('Problem with request: ' + e.message);
  });
  request.end();
});

app.use(express.static(__dirname + '/public'));

var server = app.listen(3000, function () {
  var host = server.address().address;
  var port = server.address().port;

  console.log('Example app listening at http://%s:%s', host, port);
});