Created
June 1, 2017 07:39
-
-
Save pejalo/080a6dbf0a5cb1c0ce5e5528c2523265 to your computer and use it in GitHub Desktop.
Firebase function for dynamic routing via redirect (essentials)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const admin = require('firebase-admin'); | |
function buildHtmlWithPost (post) { | |
const string = '<!DOCTYPE html><head>' \ | |
'<title>' + post.title + ' | Example Website</title>' \ | |
'<meta property="og:title" content="' + post.title + '">' \ | |
'<meta property="twitter:title" content="' + post.title + '">' \ | |
'<link rel="icon" href="https://example.com/favicon.png">' \ | |
'</head><body>' \ | |
'<script>window.location="https://example.com/?post=' + post.id + '";</script>' \ | |
'</body></html>'; | |
return string; | |
} | |
module.exports = function(req, res) { | |
const path = req.path.split('/'); | |
const postId = path[2]; | |
admin.database().ref('/posts').child(postId).once('value').then(snapshot => { | |
const post = snapshot.val(); | |
post.id = snapshot.key; | |
const htmlString = buildHtmlWithPost(post); | |
res.status(200).end(htmlString); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment