fastify-url
A plugin for fastify for accessing an incoming request's URL data.
fastify-url
is inspired by fastify-url-data
and is just a thin wrapper around Node's URL object.
Usage
const fastify = require('fastify')();
fastify.register(require('fastify-url').default);
fastify.get('/*', (req, reply) => {
const url = req.url();
req.log.info(url.host);
req.log.info(url.hostname);
req.log.info(url.href);
req.log.info(url.origin);
req.log.info(url.password);
req.log.info(url.pathname);
req.log.info(url.port);
req.log.info(url.protocol);
req.log.info(url.search);
req.log.info(url.username);
req.log.info(req.url('pathname'));
reply.send();
});
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 class and fastify-url-data
uses 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.