/hack-or-snooze-api

A HackerNews-ish clone with full CRUD on users and stories resources.

Primary LanguageJavaScript

Hack or Snooze API

Live URL

This API is currently running here: https://hack-or-snooze.herokuapp.com/

Documentation

Full interactive API documentation available here: https://hackorsnoozeapi.docs.apiary.io

Development

Prerequisites to Running Locally

  1. Install Node
  2. Install MongoDB
  3. (For Testing) Install Dredd npm i dredd -g

How to Run Locally

  1. npm i
  2. In a new tab, mongod (unless it's already running, e.g. brew services start mongodb)
  3. In the first tab, npm run dev

Server runs on http://localhost:5000 by default.

You can also pass any environment variables in a .env file.

How to Run Tests

Dredd tests run by reading the documentation blueprint against a running server. It must be installed globally to run tests.

npm i -g dredd

After starting the database and server, run the commmand:

dredd documentation.apib http://localhost:5000 -f ./tests/api-hooks.js

Notes on Data Model

We have two main entities: User and Story.

1 User has many stories. 1 User also has many favorites (which are stories).

User stories and favorites exist as arrays of Mongoose refs which are populated in the retrieval methods.

Each Story document maintains a username, which is not a DB reference. This is because Story does not need to know about User in the sense that you would never query a list of stories and have embedded user profiles, whereas the inverse (show stories embedded on User profiles) makes more sense.

Also, the routing (and consequently, much of the querying) is based on usernames, which are user-generated, and storyIds, which are auto-generated by the server. At no point should the MongoDB _id / primary key be exposed to the API users.