Parse building id server-side

This commit is contained in:
Tom Russell 2018-09-10 23:32:56 +01:00
parent 3e10119dc4
commit 6a150ba6f9
3 changed files with 28 additions and 11 deletions

11
app/package-lock.json generated
View File

@ -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",

View File

@ -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",

View File

@ -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(
<StaticRouter context={context} location={req.url}>
<App user={data.user} />
<App user={data.user} building={data.building} />
</StaticRouter>
);
@ -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) {