location-middleware
v1.1.1
Published
Getting the location URL of the current call in the request object
Downloads
2
Readme
Location Middleware
This package includes a set of scripts that makes it easy to get the location URL of the current call to your NodeJS server.
Install
// Using NPM
npm i location-middleware
// Using Yarn
yarn add location-middleware
Use
Manual
const { getLocation } = require('location-middleware');
const location = getLocation('https://username:[email protected]/my/path?foo=bar#baz');
Express
const express = require('express');
const { expressLocation } = require('location-middleware');
const app = express();
app.use(expressLocation);
app.all('/my-route', (req, res) => {
// Use `req.location`
});
Content
This package uses the default url
NodeJS module. So all the properties you will get are the ones described here: NodeJS URL + the query object listing all query params with their values.
In short, if using https://username:[email protected]/my/path?foo=bar#baz
:
{
href: 'https://username:[email protected]:3000/my/path?foo=bar&foo=baz#baz',
origin: 'https://www.example.com:3000',
protocol: 'https:',
host: 'www.example.com:3000',
hostname: 'www.example.com',
port: '3000',
username: 'username',
password: 'password',
pathname: '/my/path',
search: '?foo=bar',
query: {
foo: 'bar'
},
hash: '#baz'
}
Query params
The query params can be given in multiple ways, here is how this package deals with it (it uses the same package than express
: qs
).
// ?foo=bar
{
foo: 'bar';
}
// ?foo=bar&foo=baz
{
foo: ['bar', 'baz'];
}
// ?foo[]=bar&foo[]=baz
{
foo: ['bar', 'baz'];
}
// ?foo[0]=bar&foo[1]=baz
{
foo: ['bar', 'baz'];
}
// ?foo[value1]=bar&foo[value2]=baz
{
foo: {
value1: 'bar',
value2: 'baz'
}
}
For a full list of options, please refer to the qs package.