I am trying to visualize points on a map through NodeJs. Here is my index.js file:
var express = require('express');
var fs = require('fs');
var getdata = require('./database.js');
var app = express();
app.get('/', function (req, res, next) {
res.writeHead(200,{'Content-Type':'text/html'});
var myReadStream = fs.createReadStream(__dirname + '/index.html','utf8')
myReadStream.pipe(res);
//var value = getdata.deneme();
//console.log("Value: "+value);
});
app.listen(4000, function () {
console.log('Server is running.. on Port 4000');
});
I can call the map through HTML file. Index.html file is here:
<html><body>
<div id="mapdiv"></div>
<script src="http://www.openlayers.org/api/OpenLayers.js"></script>
<script>
map = new OpenLayers.Map("mapdiv");
map.addLayer(new OpenLayers.Layer.OSM());
var lonLat = new OpenLayers.LonLat(32 ,39 )
.transform(
new OpenLayers.Projection("EPSG:4326"), // transform from WGS 1984
map.getProjectionObject() // to Spherical Mercator Projection
);
var zoom=16;
var markers = new OpenLayers.Layer.Markers( "Markers" );
map.addLayer(markers);
markers.addMarker(new OpenLayers.Marker(lonLat));
map.setCenter (lonLat, zoom);
</script>
</body></html>
The last step is connecting to database. I created a database.js file:
const{Pool,Client} = require('pg');
var connectionString = "postgressql://ID:[email protected]:5432/dduigib0uc8ebt?ssl=true";
const pool = new Pool({
connectionString: connectionString,
});
module.exports = {
deneme: function() {
pool.query('SELECT st_astext(loc) from GetAllData()', (err, res) => {
console.log(err, res)
return res
pool.end()
});
}
};
I can connect to database and get the locations of points. But I don't know how to send these points to my HTML file to visualize the points on map. In HTML page includes sample point for visualize. I just want to enter my points to there. How can I do that ?