Advertisement
Guest User

Untitled

a guest
Feb 4th, 2019
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. var express = require('express');
  2. var router = express.Router();
  3.  
  4. /* PostgreSQL and PostGIS module and connection setup */
  5. const { Client, Query } = require('pg');
  6.  
  7. // Setup connection
  8. var username = "tiagocarvalhido" // sandbox username
  9. var password = "techdech12" // read only privileges on our table
  10. var host = "localhost:5432"
  11. var database = "is" // database name
  12. var conString = "postgres://"+username+":"+password+"@"+host+"/"+database; // Your Database Connection
  13.  
  14. // Set up your database query to display GeoJSON
  15. var coffee_query = "SELECT row_to_json(fc) FROM ( SELECT 'FeatureCollection' As type, array_to_json(array_agg(f)) As features FROM (SELECT 'Feature' As type, ST_AsGeoJSON(lg.geom)::json As geometry, row_to_json((gid, lug11desig)) As properties FROM caop As lg) As f) As fc";
  16. /* GET home page. */
  17. router.get('/', function(req, res, next) {
  18. res.render('index', { title: 'Express' });
  19. });
  20.  
  21. /* GET Postgres JSON data */
  22. router.get('/data', function (req, res) {
  23. var client = new Client(conString);
  24. client.connect();
  25. var query = client.query(new Query(coffee_query));
  26. query.on("row", function (row, result) {
  27. result.addRow(row);
  28. });
  29. query.on("end", function (result) {
  30. res.send(result.rows[0].row_to_json);
  31. res.end();
  32. });
  33. });
  34.  
  35.  
  36.  
  37.  
  38.  
  39. router.post('/marker', function (req, res) {
  40. var marker = req.body;
  41. console.log(marker);
  42. var client = new Client(conString);
  43. client.connect();
  44. var query = `INSERT INTO mypoints(geom) VALUES( ST_GeomFromText('POINT(${marker.marker.lng} ${marker.marker.lat})', 4326))`
  45. client.query(query, function(err, result){
  46. if(err){
  47. console.log(err);
  48. } else {
  49. console.log(result);
  50. }
  51. });
  52. });
  53.  
  54.  
  55.  
  56.  
  57. module.exports = router;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement