Giter Site home page Giter Site logo

url-shortener's Introduction

URL Shortener Server

This is an implementation of URL Shortener Server, where clients can pass long URL and get back shortened version of it.

URL-Shortener-Demo.mov

Requirements

The server is written in Node v17 and Express v4. You will also need the MongoDB database to run the server.

Setup

Clone the repo by doing

git clone https://github.com/PallaviTS/URL-Shortener.git

With Node, NPM and MongoDB installed on your system you can install the application locally using

$ npm install

The application uses a local MongoDB database. You can update the DATABASE_URL in .env file to work with a different DB.

Express comes with a local development server. To run the application use

$ npm start

Which starts the application and lets it listen on port 3000.

You can also run the test suite with

$ npm run test

and linter with

$ npm run lint

Usage

With the server running you can make HTTP requests to it in order to create short URLs and call them afterwards.

The URL Shortener server provides one resource endpoint /shortUrls that allows you to list, show, and create urls. The server handles JSON payloads and an endpoint definition might look like this:

Create a new short URL

curl -d '{"fullUrl":"https://www.monkeyuser.com/2019/bug-fixing-ways/"}' -H "Content-Type: application/json" -X POST http://localhost:3000/shortUrls

will return unique short url with tier.app- as base

{
    "full": "https://www.monkeyuser.com/2019/bug-fixing-ways",
    "short": "tier.app-_RgWjL-tt"
}

Get all short URLs with visits

curl -X GET http://localhost:3000/shortUrls

will return

[
    {
        "visitCount": 1,
        "full": "https://www.monkeyuser.com/2019/bug-fixing-ways",
        "short": "tier.app-_RgWjL-tt"
    }
]

Get a short URL by short ID

curl -X GET http://localhost:3000/shortUrls/tier.app-_RgWjL-tt

will redirect https://www.monkeyuser.com/2019/bug-fixing-ways/

Errors and Validation

The server implements some validation:

  • Only GET /shortUrls, POST /shortUrls and GET /shortUrls/:shortUrl are allowed, all other Path and Method combinations respond with 405 error.
  • Full URL in POST request needs to be valid and present
  • Server returns error stacktrace only for dev environment and on production no data is leaked

When validation fails or when short URL cannot be found, the server responds with message, for example:

GET /shortUrls/10

404 Not Found
{
    "message": "10 Not found"
}
POST /shortUrls with Body {"fullUrl":"not an URL"}

400 Not Found

{
    "message": "Full url is invalid",
    "error": {
        "status": 400
    }
}

Possible improvements

  • When accessing an existing endpoint path with a different HTTP method the server will respond with 405 Method Not Allowed, we could add the Allow response header with the method(s) currently supported
  • Error messages, especially for endpoint path/method combinations not allowed, can be improved to include path/method in response
  • Sucess response, could include ids and respond with 201 status code depending on clients requirement

url-shortener's People

Contributors

pallavits avatar

Watchers

James Cloos avatar  avatar

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.