# fastify-url

> Fastify plugin for parsing and attaching URL data to requests

Latest version **0.2.3** (published 2019-09-02) · MIT license · 0 weekly downloads

## Install

```sh
npm install fastify-url
pnpm add fastify-url
yarn add fastify-url
bun add fastify-url
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.3 |
| Published | 2019-09-02 |
| First published | 2019-04-07 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 7.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Sam Faulkner |
| Maintainers | therealsamf |
| Keywords | url, fastify, nodejs |

## Links

- npm: https://www.npmjs.com/package/fastify-url
- npm.io page: https://npm.io/package/fastify-url

## Dependencies (1)

- [fastify-plugin](https://npm.io/package/fastify-plugin.md) ^1.5.0

## Alternatives

- [base64url](https://npm.io/package/base64url.md) — 6.1M weekly downloads
- [get-installed-path](https://npm.io/package/get-installed-path.md) — 502.9K weekly downloads
- [@uppy/url](https://npm.io/package/@uppy/url.md) — 185.8K weekly downloads
- [@d3fc/d3fc-shape](https://npm.io/package/@d3fc/d3fc-shape.md) — 16.2K weekly downloads
- [localizer](https://npm.io/package/localizer.md) — 226 weekly downloads

## Recent versions

- 0.2.3 (latest) — 2019-09-02
- 0.2.2 — 2019-05-20
- 0.2.1 — 2019-04-28
- 0.2.0 — 2019-04-09
- 0.1.2 — 2019-04-07
- 0.1.1 — 2019-04-07
- 0.1.0 — 2019-04-07

## README

fastify-url
===========

[![NPM](https://img.shields.io/npm/v/fastify-url.svg)](https://www.npmjs.com/package/fastify-url)
[![Build Status](https://travis-ci.org/therealsamf/fastify-url.svg?branch=master)](https://travis-ci.org/therealsamf/fastify-url)
[![codecov.io Code Coverage](https://img.shields.io/codecov/c/github/therealsamf/fastify-url.svg?maxAge=2592000)](https://codecov.io/github/therealsamf/fastify-url?branch=master) [![Greenkeeper badge](https://badges.greenkeeper.io/therealsamf/fastify-url.svg)](https://greenkeeper.io/)



A plugin for [fastify](https://www.fastify.io/) for accessing an incoming [request](https://www.fastify.io/docs/latest/Request/)'s URL data.

`fastify-url` is inspired by [`fastify-url-data`](https://github.com/fastify/fastify-url-data) and is just a thin wrapper around Node's [URL](https://nodejs.org/api/url.html#url_class_url) object.

## Usage

```javascript
const fastify = require('fastify')();

fastify.register(require('fastify-url').default);

fastify.get('/*', (req, reply) => {
  const url = req.url();

  req.log.info(url.host);          // 'sub.example.com:8080'
  req.log.info(url.hostname);      // 'sub.example.com'
  req.log.info(url.href);          // 'http://user:pass@sub.example.com:8080/p/a/t/h?query=string'
  req.log.info(url.origin);        // 'http://sub.example.com:8080'
  req.log.info(url.password);      // 'pass'
  req.log.info(url.pathname);      // '/p/a/t/h'
  req.log.info(url.port);          // '8080'
  req.log.info(url.protocol);      // 'http:'
  req.log.info(url.search);        // '?query=string'
  req.log.info(url.username);      // 'user'

  // if you just need single data:
  req.log.info(req.url('pathname')); // '/p/a/t/h'

  reply.send();
});

// GET: 'http://user:pass@sub.example.com:8080/p/a/t/h?query=string'
```

## Options

### protocol

Type: `string`
Default: `http`

This property allows you to change the protocol the incoming request's URL object will absorb. This is used because it's difficult to find the protocol the request was received from within the request handler.

## `fastify-url` vs `fastify-url-data`

The difference between these two plugins is `fastify-url` uses the native NodeJS [URL](https://nodejs.org/api/url.html#url_class_url) class and `fastify-url-data` uses [`uri-js`](https://github.com/garycourt/uri-js). These implementations provide some of the same features but have different data members. Depending on your requirements you may need one or the other, but using both is redundant.

---
_Source: https://npm.io/package/fastify-url · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
