Advertisement
Guest User

Untitled

a guest
Feb 3rd, 2019
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. var express = require('express'); // require Express
  2. var router = express.Router(); // setup usage of the Express router engine
  3. const bodyparser = require('body-parser');
  4.  
  5. /* PostgreSQL and PostGIS module and connection setup */
  6. const { Client, Query } = require('pg');
  7. var pg = require("pg");
  8.  
  9.  
  10.  
  11. // Setup connection
  12. var username = "tiagocarvalhido" // sandbox username
  13. var password = "techdech12" // read only privileges on our table
  14. var host = "localhost:5432"
  15. var database = "is" // database name
  16. var conString = "postgres://"+username+":"+password+"@"+host+"/"+database; // Your Database Connection
  17.  
  18. // Set up your database query to display GeoJSON
  19. var caop = "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";
  20.  
  21.  
  22. router.get('/', function(req, res, next) {
  23. res.render('index', { title: 'Express' });
  24. });
  25.  
  26. module.exports = router;
  27.  
  28.  
  29.  
  30.  
  31.  
  32. /* GET Postgres JSON data */
  33. router.get('/data', function (req, res) {
  34. var client = new Client(conString);
  35. client.connect();
  36. var query = client.query(new Query(caop));
  37. query.on("row", function (row, result) {
  38. result.addRow(row);
  39. });
  40. query.on("end", function (result) {
  41. res.send(result.rows[0].row_to_json);
  42. res.end();
  43. });
  44. });
  45.  
  46.  
  47. /* GET the map page */
  48. router.get('/map', function(req, res) {
  49. var client = new Client(conString); // Setup our Postgres Client
  50. client.connect(); // connect to the client
  51.  
  52. var query = client.query(new Query(caop)); // Run our Query
  53.  
  54. query.on("row", function (row, result) {
  55.  
  56. result.addRow(row);
  57. });
  58.  
  59. // Pass the result to the map page
  60.  
  61. query.on("end", function (result) {
  62. var data = result.rows[0].row_to_json // Save the JSON as variable data
  63. res.render('map', {
  64. title: "NodeJs + Leafleat", // Give a title to our page
  65. jsonData: data // Pass data to the View
  66. });
  67. });
  68.  
  69.  
  70. });
  71.  
  72.  
  73.  
  74.  
  75.  
  76. var inserir = "select row_to_json(ponto) from points";
  77.  
  78. router.get('/ponto', function (req, res) {
  79. var client = new Client(conString);
  80. client.connect();
  81. var query = client.query(new Query(inserir));
  82. query.on("row", function (row, result) {
  83. result.addRow(row);
  84. });
  85. query.on("end", function (result) {
  86. res.send(result.rows[0].row_to_json);
  87. res.end();
  88. });
  89. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement