Commit 7e245871 authored by Jamie Tanna's avatar Jamie Tanna

Document parsing URL fragments with Node

Closes #630.
parent d526048c
title: "Parsing Key-Value URL Fragments with Node.JS"
description: "How to easily parse a URL fragment containing key-value pairs of data, with Node.JS."
- nodejs
- blogumentation
license_code: Apache-2.0
license_prose: CC-BY-NC-SA-4.0
date: 2019-08-01T18:08:15+0100
slug: "node-parse-url-fragment"
image: /img/vendor/nodejs.png
Although the URL fragment isn't defined explicitly as a key-value pair, there are times where it may be, for instance during the [OAuth2 Implicit Grant]( or the [OpenID Connect Hybrid Flow](
We can use the `url` module to actually parse our URL out, and then the `querystring` module to extract the key-value parameters for the fragment, instead of hand-rolling our own:
const url = require('url');
const querystring = require('querystring');
const theUrl = '';
var fragmentParams = querystring.parse(url.parse(theUrl).hash.replace('#', ''));
// Object: null prototype] { code: 'foo', state: 'blah' }
Also note that yes, this should be received by your browser, not by some Node code which runs on the backend, but for the purpose I've been testing it with, the Node code for it was important.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment