From 6a150ba6f998e7b44a73056201e5928a6e21fe50 Mon Sep 17 00:00:00 2001 From: Tom Russell Date: Mon, 10 Sep 2018 23:32:56 +0100 Subject: [PATCH] Parse building id server-side --- app/package-lock.json | 11 ++++++++--- app/package.json | 1 + app/src/server.js | 27 +++++++++++++++++++-------- 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/app/package-lock.json b/app/package-lock.json index 2bbe2784..8ddc11b1 100644 --- a/app/package-lock.json +++ b/app/package-lock.json @@ -4138,6 +4138,11 @@ "type-is": "~1.6.15" } }, + "path-to-regexp": { + "version": "0.1.7", + "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-0.1.7.tgz", + "integrity": "sha1-32BBeABfUi8V60SQ5yR6G/qmf4w=" + }, "raw-body": { "version": "2.3.2", "resolved": "https://registry.npmjs.org/raw-body/-/raw-body-2.3.2.tgz", @@ -8316,9 +8321,9 @@ "integrity": "sha512-GSmOT2EbHrINBf9SR7CDELwlJ8AENk3Qn7OikK4nFYAu3Ote2+JYNVvkpAEQm3/TLNEJFD/xZJjzyxg3KBWOzw==" }, "path-to-regexp": { - "version": "0.1.7", - "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-0.1.7.tgz", - "integrity": "sha1-32BBeABfUi8V60SQ5yR6G/qmf4w=" + "version": "2.4.0", + "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-2.4.0.tgz", + "integrity": "sha512-G6zHoVqC6GGTQkZwF4lkuEyMbVOjoBKAEybQUypI1WTkqinCOrq2x6U2+phkJ1XsEMTy4LjtwPI7HW+NVrRR2w==" }, "path-type": { "version": "3.0.0", diff --git a/app/package.json b/app/package.json index 6ccb7b65..2fbcd011 100644 --- a/app/package.json +++ b/app/package.json @@ -19,6 +19,7 @@ "express-session": "^1.15.6", "leaflet": "^1.3.4", "mapnik": "^4.0.1", + "path-to-regexp": "^2.4.0", "razzle": "2.4.0", "react": "16.4.2", "react-dom": "16.4.2", diff --git a/app/src/server.js b/app/src/server.js index b2c819f6..36e459e4 100644 --- a/app/src/server.js +++ b/app/src/server.js @@ -3,6 +3,7 @@ import { StaticRouter } from 'react-router-dom'; import express from 'express'; import { renderToString } from 'react-dom/server'; import serialize from 'serialize-javascript'; +import pathToRegexp from 'path-to-regexp'; const bodyParser = require('body-parser') const session = require('express-session') @@ -37,6 +38,7 @@ const sess = { tableName : 'user_sessions' }), secret: process.env.APP_COOKIE_SECRET, + saveUninitialized: false, resave: false, cookie: { maxAge: 30 * 24 * 60 * 60 * 1000 } // 30 days }; @@ -53,25 +55,34 @@ server.get('/*.html', frontendRoute); server.get('/', frontendRoute); function frontendRoute(req, res) { - const context = {}; const data = {}; + var re = pathToRegexp('/buildings/:building.html') + var matches = re.exec(req.url) + + var building_id = undefined; + if (matches && matches.length === 2) { + building_id = matches[1] + } + console.log(`Building: ${building_id}`) + if (req.session.user_id) { getUserById(req.session.user_id).then(function(user){ data.user = user; - console.log(user); - renderHTML(context, data, req, res) + renderHTML(data, req, res) }).catch(function(){ - renderHTML(context, data, req, res); + renderHTML(data, req, res); }); } else { - renderHTML(context, data, req, res); + // getBuildingById() TODO load data server-side + renderHTML(data, req, res); } } -function renderHTML(context, data, req, res){ +function renderHTML(data, req, res){ + const context = {}; const markup = renderToString( - + ); @@ -122,7 +133,7 @@ server.use('/tiles', tileserver); // GET building at point -server.get('/buildings', function(req, res){ +server.get('/buildings.json', function(req, res){ const { lng, lat } = req.query queryBuildingAtPoint(lng, lat).then(function(result){ if (result) {