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."
tags:
- 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](https://tools.ietf.org/html/rfc6749#section-4.2) or the [OpenID Connect Hybrid Flow](https://openid.net/specs/openid-connect-core-1_0.html#HybridFlowAuth).
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:
```js
const url = require('url');
const querystring = require('querystring');
const theUrl = 'https://example.com/auth/callback#code=foo&state=blah';
var fragmentParams = querystring.parse(url.parse(theUrl).hash.replace('#', ''));
console.log(fragmentParams);
// 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.
---
posts:
- https://www.jvt.me/posts/2019/08/01/node-parse-url-fragment/
- https://www.jvt.me/posts/2019/07/27/rsvp-calendar/
- https://www.jvt.me/posts/2019/07/27/meetupcom-calendar/
- https://www.jvt.me/posts/2019/07/24/pretty-print-json-files-inline/
......
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