从网站API检索数据并显示到EJS中

时间:2018-12-28 18:17:52

标签: json sails.js ejs geojson

我正在尝试使用网站API检索数据,因此可以将其插入到Leaflet Map应用程序中。 就目前而言,我正在使用https get方法来检索它,并且在运行时它会在控制台日志中返回 undefined

我的方法是检索此数据,将其解析为JSON,然后将其插入到我的EJS文件中。我目前正在使用带有节点的Sails.js。

到目前为止,这是我的代码:

const https = require('https');

https.get('https://thesession.org/sessions/nearby?latlon=53,-6&radius=75&format=json&perpage=50', (resp) => {
    let data = '';

    // A chunk of data has been recieved.
    resp.on('data', (chunk) => {
       data += chunk;
    });

    // The whole response has been received. Print out the result.
    resp.on('end', () => {
        console.log(JSON.parse(data).explanation);
    });

}).on("error", (err) => {
    console.log("Error: " + err.message);
});
<body>
<div id="mapid"></div>
</body>

<script src="https://unpkg.com/leaflet@1.3.4/dist/leaflet.js"
        integrity="sha512-nMMmRyTVoLYqjP9hrbed9S+FzjZHW5gY1TWCHA5ckwXZBadntCNs8kEqAWdrb9O7rxbCaA4lKTIWjDXZxflOcA=="
        crossorigin=""></script>
<script type="text/javascript" src="leaflet.ajax.min.js"></script>
<script>
    var mymap = L.map('mapid').setView([53.513342, -6.541414], 13);

    L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token={accessToken}', {
        attribution: 'Map data &copy; <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors, <a href="https://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="https://www.mapbox.com/">Mapbox</a>',
        maxZoom: 18,
        id: 'mapbox.streets',
        accessToken: 'pk.eyJ1IjoiaWlyb3VnZWlpIiwiYSI6ImNqbmQzaDR1bDA5YWczcWxmOHhhM2prYXkifQ._l5K7TrnSck_TvRqFq4GAw'
    }).addTo(mymap);


    var popup = L.popup();

    function onMapClick(e) {
        popup
            .setLatLng(e.latlng)
            .setContent("You clicked the map at " + e.latlng.toString())
            .openOn(mymap);
    }

    mymap.on('click', onMapClick);

    var geojsonLayer = new L.GeoJSON.AJAX("Points.geojson");
</script>

谢谢。

0 个答案:

没有答案