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
.
src/server
contains logic for the server and simple router.src/models
interacts with postgress database models.src/controllers
http controllers for 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 |
To get project running locally with Docker, run
yarn docker:serve
# alias for "docker-compose -f docker-compose.local.yml up"
- Node.js
16.x
- Docker or PostgreSQL
-
Install dependecies
npm install
-
Start Postgres database
npm run docker:db # or use a local service
-
Export envs
export DATABASE_URL='postgresql://postgres:postgres@127.0.0.1:5432/main' # or modify according to your local database
-
Run database migrations
npm run db:migrate
-
Start development server with watch mode
npm run dev
-
Start coding
For unit & integration tests the project uses Jest. Tests are in test
directory.
To run unit tests run command
npm run test
Project uses TypeScript.
To run type checking across the whole project, run command
npm run typecheck
Project uses ESLint for linting. That is configured in .eslintrc.js
.
To run linting with auto-fix, run command
npm run lint
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
Service is hosted in Fly.io.
Deployment to production is made via GitHub actions on push to main
branch.