1.0.31 • Published 2 years ago

express-shortener v1.0.31

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

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)

1.0.31

2 years ago

1.0.30

2 years ago

1.0.29

2 years ago

1.0.28

2 years ago

1.0.27

2 years ago

1.0.26

2 years ago

1.0.25

2 years ago

1.0.24

2 years ago

1.0.23

2 years ago

1.0.22

2 years ago

1.0.21

2 years ago

1.0.20

2 years ago

1.0.19

2 years ago

1.0.18

2 years ago

1.0.17

2 years ago

1.0.16

2 years ago

1.0.15

2 years ago

1.0.14

2 years ago

1.0.13

2 years ago

1.0.12

2 years ago

1.0.11

2 years ago

1.0.10

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago