repl.it
@StefanGisi/

map-validator

Express

No description

fork
loading
Files
  • index.js
  • public
  • Packager files
  • package-lock.json
  • package.json
index.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
const express = require('express');
const bodyParser = require('body-parser');

const app = express();
const path = require('path');

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

app.get('/', (req, res, next) => {
  
  var addressTest = req.query.address;

  if (!addressTest) {


    app.use(express.static('public'));
    
    app.use(express.static(path.join(__dirname, 'public')));

    res.redirect(__dirname + '/../../index.html');

  } else {

    var address = req.query.address; // $_GET["id"]
    address = encodeURIComponent(address);

    var https = require('https');
    var options = {
      host: 'maps.googleapis.com',
      path: '/maps/api/geocode/json?address=' + address + "&key=AIzaSyB0u9D3dZT4a4sNNt0b-WBEIf13RTgXuis"
    };

    //map_ req, res vs. req, res (for headers)

    app.use(function(map_req, map_res, map_next) {
      res.header("Access-Control-Allow-Origin", "*");
      res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
      next();
    });

    var map_req = https.get(options, function(map_res) {

      // Buffer the body entirely for processing as a whole.
      var bodyChunks = [];
      map_res.on('data', function(chunk) {

        // You can process streamed parts here...
        bodyChunks.push(chunk);

      }).on('end', function() {
        
        address = decodeURIComponent(address);
        address = address.split("+").join(" ");

        var html = '<div class="mt-4 mb-4 text-primary">You typed <b>' + address + '</b> for your address.</div><hr>';

        var jsonResp = Buffer.concat(bodyChunks);
        
        jsonResp = JSON.parse(jsonResp);

        var place_id = jsonResp.results[0].place_id;
        console.log(place_id);

        var place_iframe = '<iframe width="400" height="300" frameborder="0" style="border:0;" src="https://www.google.com/maps/embed/v1/place?key=AIzaSyB0u9D3dZT4a4sNNt0b-WBEIf13RTgXuis&q=place_id:' + place_id + '"  allowfullscreen></iframe>';

        console.log(place_iframe);

        html += place_iframe;

        res.send(html);

      })
      
    })

    map_req.on('error', function(e) {
      console.log('ERROR: ' + e.message);
    });

  }

});

app.listen(3000, () => console.log('server started'));