/backend-API-accessment

hatchway accessment

Primary LanguageJavaScript

An accessment from hatchway, everything in posts.js was implemented to fullfill the requirements.

Getting Started

  • System requirements
    • Node.JS v16
  • Set environment variables: create a .env file in the root directory, and copy the contents from .env.sample
  • Install dependencies
    yarn
    
  • Seed database
    yarn seed
    
  • Start the dev server
    yarn dev
    

Getting Started (Docker)

Instead of following the steps above, you can also use Docker to set up your environment.

  • System requirements
  • Run docker-compose up to spin up the dev server.
  • Enter Ctrl-C in the same same terminal or docker-compose down in a separate terminal to shut down the server.

Verify That Everything Is Set Up Correctly

You can use cURL or a tool like Postman to test the API.

Example Curl Commands

You can log in as one of the seeded users with the following curl command:

curl --location --request POST 'localhost:8080/api/login' \
--header 'Content-Type: application/json' \
--data-raw '{
    "username": "thomas",
    "password": "123456"
}'

Then you can use the token that comes back from the /login request to make an authenticated request to create a new blog post

curl --location --request POST 'localhost:8080/api/posts' \
--header 'x-access-token: your-token-here' \
--header 'Content-Type: application/json' \
--data-raw '{
    "text": "This is some text for the blog post...",
    "tags": ["travel", "hotel"]
}'

Helpful Commands

  • yarn lint : Runs linter.
  • yarn prettier --write . : Runs autoformatter
  • yarn test : This repository contains a non-comprehensive set of unit tests used to determine if your code meets the basic requirements of the assignment. Please do not modify these tests.
  • yarn seed : Resets database and populates it with sample data.

Common Setup Errors

  • If you encounter an error related to secretOrPrivateKey when attempting to make a request, please ensure you have created a .env file in the root directory as per the Getting Started instructions.
  • If you are on M1 mac, you might encounter ERR_DLOPEN_FAILED when you try to install dependencies locally then run the server in docker (or vice versa). In case of error, try removing the node_modules directory and restart docker compose up.