Giter Club home page Giter Club logo

location-middleware's Introduction

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.