npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

express-shortener

v1.0.31

Published

A package you can use to create a URL Shortener with express

Downloads

44

Readme

URL Shortener Package

A package you can use to create a URL Shortener with express

Installation progress

The package itself does not need any external packages, you only need packages when you try to execute the package

Features

  • Simple design
  • Advanced error logging

Create a database

Create a JSON File with an empty object

{}

Documentation

Setup

const express = require("express"); // this package is express server based
const app = express();
const bodyParser = require("body-parser");
const urlShortener = require('express-shortener');

app.set('trust proxy', true); // required if you enabled "logClicks"
app.use(bodyParser.json()); // required for post requests
app.use((req, res, next) => {
  res.setHeader('X-Powered-By', 'Dinoscape');
  next();
});

Configuration

urlShortener.configURL({
  file: "./urls.json",
  parameter: "id",
  logClicks: true // optional
}).then((result) => {
  console.log("Options were approved to the package");
});

Open URL

app.all("/url/:id", (req, res) => {
  urlShortener.openURL(req, res).then((result) => {});
});

Create URL

app.all("/api/v1/url/create", (req, res) => {
  urlShortener.createURL(req.body).then((result) => {
    res.json(result);
  });
});

Edit URL

app.all("/api/v1/url/edit", (req, res) => {
  urlShortener.editURL(req.body).then((result) => {
    res.json(result);
  });
});

Delete URL

app.all("/api/v1/url/delete", (req, res) => {
  urlShortener.deleteURL(req.body).then((result) => {
    res.json(result);
  });
});

Get URL

app.all("/api/v1/url/get", (req, res) => {
  urlShortener.getURL(req.body).then((result) => {
    res.json(result);
  });
});

Listen to Server

app.listen(3000, () => {
  console.log("Server is ready");
});

Example

const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const urlShortener = require('./urlShortener.js');

urlShortener.configURL({
  file: "./urls.json",
  parameter: "id",
  logClicks: true
}).then((result) => {
  console.log("URL Shorter options were approved to the package");
});

app.set('trust proxy', true);
app.use(bodyParser.json());
app.use((req, res, next) => {
  res.setHeader('X-Powered-By', 'Dinoscape');
  next();
});

app.all("/url/:id", (req, res) => {
  urlShortener.openURL(req, res).then((result) => {});
});

app.all("/api/v1/url/create", (req, res) => {
  urlShortener.createURL(req.body).then((result) => {
    res.json(result);
  });
});

app.all("/api/v1/url/edit", (req, res) => {
  urlShortener.editURL(req.body).then((result) => {
    res.json(result);
  });
});

app.all("/api/v1/url/delete", (req, res) => {
  urlShortener.deleteURL(req.body).then((result) => {
    res.json(result);
  });
});

app.all("/api/v1/url/get", (req, res) => {
  urlShortener.getURL(req.body).then((result) => {
    res.json(result);
  });
});

app.listen(3000, () => {
  console.log("Server is ready");
});

Class Documentation

Setup

const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const urlShortener = require('express-shortener');
const Shortener = new urlShortener.Shortener({
  file: "./urls.json",
  parameter: "id",
  logClicks: true // optional
});

app.set('trust proxy', true); // required if you enabled "logClicks"
app.use(bodyParser.json()); // required for post requests
app.use((req, res, next) => {
  res.setHeader('X-Powered-By', 'Dinoscape');
  next();
});

Open URL

app.all("/url/:id", Shortener.open);

Create URL

app.all("/api/v1/url/create", Shortener.create);

Edit URL

app.all("/api/v1/url/edit", Shortener.edit);

Delete URL

app.all("/api/v1/url/delete", Shortener.delete);

Get URL

app.all("/api/v1/url/get", Shortener.get);

Listen to Server

app.listen(3000, () => {
  console.log("Server is ready");
});

Example

const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const urlShortener = require('express-shortener');
const Shortener = new urlShortener.Shortener({
  file: "./urls.json",
  parameter: "id",
  logClicks: true
});

app.set('trust proxy', true);
app.use(bodyParser.json());
app.use((req, res, next) => {
  res.setHeader('X-Powered-By', 'Dinoscape');
  next();
});

app.all("/url/:id", Shortener.open);

app.all("/api/v1/url/create", Shortener.create);

app.all("/api/v1/url/edit", Shortener.edit);

app.all("/api/v1/url/delete", Shortener.delete);

app.all("/api/v1/url/get", Shortener.get);

app.listen(3000, () => {
  console.log("Server is ready");
});

Middleware Documentation

Setup

const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const urlShortener = require('express-shortener');

app.set('trust proxy', true);
app.use(bodyParser.json());
app.use(urlShortener.shortener({
  file: "./urls.json",
  parameter: "id",
  logClicks: true
}));

app.use((req, res, next) => {
  res.setHeader('X-Powered-By', 'Dinoscape');
  next();
});

Open URL

app.all("/url/:id", (req, res) => {
  res.openURL();
});

Create URL

app.all("/api/v1/url/create", (req, res) => {
  res.createURL();
});

Edit URL

app.all("/api/v1/url/edit", (req, res) => {
  res.editURL();
});

Delete URL

app.all("/api/v1/url/delete", (req, res) => {
  res.deleteURL();
});

Get URL

app.all("/api/v1/url/get", (req, res) => {
  res.getURL();
});

Listen to Server

app.listen(3000, () => {
  console.log("Server is ready");
});

Documentation

const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const urlShortener = require('express-shortener');

app.set('trust proxy', true);
app.use(bodyParser.json());
app.use(urlShortener.shortener({
  file: "./urls.json",
  parameter: "id",
  logClicks: true
}));

app.use((req, res, next) => {
  res.setHeader('X-Powered-By', 'Dinoscape');
  next();
});

app.all("/url/:id", (req, res) => {
  res.openURL();
});

app.post('/api/v1/url/create', (req, res) => {
  res.createURL();
});

app.post('/api/v1/url/edit', (req, res) => {
  res.editURL();
});

app.post('/api/v1/url/delete', (req, res) => {
  res.deleteURL();
});

app.post('/api/v1/url/get', (req, res) => {
  res.getURL();
});

app.listen(3000, () => {
  console.log("Server is ready");
});

Error Handling

Invalid options

The options you have given or not defined or not an object

No options were given

You have not executed the ConfigURL method before executing this method

No file was given

Your options do not contain a file

Enable Trust Proxy

You set logClicks in the options to true but you have not enabled trust proxy

app.set('trust proxy', true);

No parameter was given

Your options do not contain a but you have executed the openURL function so the package does not know what id it should open

Invalid parameter

The parameter does not exist in the request or is not defined

Id does not exist

The given id is not available in the database

No body was given

The body item was not given in the function

No url was given

The url item does not exist in the body

Invalid url

The url is not a valid

Id already exists

The custom id already exists in the database

No id was given

The id item does not exist in the body

No token was given

The token item does not exist in the body

Invalid token

The token is not valid

Other Errors

Other errors are from the node internal packages fs and crypto (mostly fs when you enter a not existing file)