Skip to content

📝 Node REST API example (API for the the-wall-client)

Notifications You must be signed in to change notification settings

jmerilainen/the-wall-api

Repository files navigation

The WALL API

This is an example of Node REST API built from scratch and with zero dependencies (except node-postgres).

Demo: https://the-wall-api.fly.dev (API), https://the-wall.fly.dev (Client).

A barebone client for the API is found in the-wall-client.

For fun: A Bun (a new js runtime) version of this is in the-wall-api-bun.

About

Strucutre

  • src/server contains logic for the server and simple router.
  • src/models interacts with postgress database models.
  • src/controllers http controllers for routes.

Routes

Method Route Action Data
GET / Hello World -
GET /posts List all posts -
POST /posts Create a new post content: string
GET /posts/:id Get a single post -
PATCH /posts/:id Modify a post content: string

Quickstart with Docker

To get project running locally with Docker, run

yarn docker:serve

# alias for "docker-compose -f docker-compose.local.yml up"

Open http://localhost:3000

Requirements

  • Node.js 16.x
  • Docker or PostgreSQL

Development

  1. Install dependecies

    npm install
  2. Start Postgres database

    npm run docker:db
    
    # or use a local service
  3. Export envs

    export DATABASE_URL='postgresql://postgres:postgres@127.0.0.1:5432/main'
    
    # or modify according to your local database
  4. Run database migrations

    npm run db:migrate
  5. Start development server with watch mode

    npm run dev
  6. Start coding

Testing

Unit & integration

For unit & integration tests the project uses Jest. Tests are in test directory.

To run unit tests run command

npm run test

Type checks

Project uses TypeScript.

To run type checking across the whole project, run command

npm run typecheck

Linting

Project uses ESLint for linting. That is configured in .eslintrc.js.

To run linting with auto-fix, run command

npm run lint

Formatting

Prettier is used for auto-formatting. It's recommended to install an editor plugin (like the VSCode Prettier plugin) to get auto-formatting on save.

To run formating, run command

npm run format

Deployment

Service is hosted in Fly.io.

Deployment to production is made via GitHub actions on push to main branch.