Code Monkey home page Code Monkey logo

snippet's Introduction

Snippet API Server

Tech: Node, Express

Getting Started

npm install npm start

Goals

Write an API web server that accepts a snippet of text and makes that snippet available at a URL. Each snippet should be available as text at a URL until it expires. A snippet's expiry should be extended by 30 seconds each time it is accessed.

The request to store the snippet should accept this information:

Name Description
name Name of the snippet
expires_in Seconds from now until the snippet should expire
snippet Contents of the snippet

The request to store the snippet should be replied to with a response that includes the URL where the snippet can be read.

Snippets can be stored in memory, and do not need to be editable after storing. The solution needs only to be an API, not a graphical or website user interface.

Snippets should be retrievable by name.

The Problem

  • An actual HTTP web server that runs and can be accessed through a URL
  • A clean, minimalistic implementation. Focus on the core functionality, and pay extra attention to API the service should serve
  • Appropriate use of web frameworks and open-source libraries as necessary. Think of this as building an MVP
  • Too little time? Your prioritization skills are also being evaluated
  • Readable and clear code, so that everyone and anyone can understand it

The Solution

Test Cases

We expect you to implement this API exactly. Please ensure your solution handles these test cases correctly.

curl -X POST -H "Content-Type: application/json" -d '{"name":"recipe", "expires_in": 30, "snippet":"1 apple"}' https://example.com/snippets
# response 201 Created
  "url": "https://example.com/snippets/recipe",
  "name": "recipe",
  "expires_at": "2020-02-22T20:02:02Z",
  "snippet": "1 apple"
}

curl https://example.com/snippets/recipe
# response 200 OK
{
  "url": "https://example.com/snippets/recipe",
  "name": "recipe",
  "expires_at": "2020-02-22T20:02:32Z",
  "snippet": "1 apple"
}

# wait 60 seconds

curl https://example.com/snippets/recipe
# response 404 Not Found

Extension

Pick one feature to implement on top of the above. Pick only one.

  1. Store snippets on disk in files.
  2. Allow editing snippets using an optional password set at the time they are stored. Editing extends expiration by 30 seconds if a new expiration is not specified.
  3. Add a "like" API endpoint that increases a counter for a snippet. Liking extends expiration by 30 seconds.

Extension Test Cases

# 1. store on disk

# The API doesn't change silly!

# 2. editing with password

# create the snippet
curl -X POST -H "Content-Type: application/json" -d '{"name":"recipe", "expires_in":30, "snippet":"1 apple", "password":"1234"}' https://example.com/snippets
# response 201 Created
{
  "url": "https://example.com/snippets/recipe",
  "name": "recipe",
  "expires_at": "2020-02-22T20:02:02Z",
  "snippet": "1 apple",
  "password": "1234"
}

# edit the snippet
curl -X PUT -H "Content-Type: application/json" -d '{"password":"1234", "snippet":"40 grapes"}' https://example.com/snippets/recipe
# response 200 OK
{
  "url": "https://example.com/snippets/recipe",
  "name": "recipe",
  "expires_at": "2020-02-22T20:02:32Z",
  "snippet": "40 grapes",
  "password": "1234"
}

# 3. like endpoint
curl -X POST https://example.com/snippets/recipe/like
# response 200 OK
{
  "url": "https://example.com/snippets/recipe",
  "name": "recipe",
  "expires_at": "2020-02-22T20:02:32Z",
  "snippet": "1 apple",
  "likes": 1
}

curl https://example.com/snippets/recipe
# response 200 OK
{
  "url": "https://example.com/snippets/recipe",
  "name": "recipe",
  "expires_at": "2020-02-22T20:03:02Z",
  "snippet": "1 apple",
  "likes": 1
}

snippet's People

Contributors

eddielthomas avatar

Watchers

 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.