Sample code demonstrating writing APIs using Node.js as a backend runtime, TypeScript for type checking, Sequelize as ORM, and Passport.js for authentication.
Setting up and developing locally is pretty straightforward.
You must have the following software/tools installed on your system:
- Docker version >= 24.0.4
- Docker Compose version >= v2.2.3
- A recent version of VS Code
- VS Code extension for API browsing: REST Client
Clone the code repository or open it in GitHub Codespaces and navigate to the project dir:
docker compose up --build
<OR>
docker-compose up --build
It will build and spin up two Docker containers: the backend API and a PostgreSQL database instance. All the credentials are sourced via environment variables in docker-compose.yml
. On app start-up, the "Users" table and a dummy "superuser" will be created automatically.
Even though the API endpoints could be tested in any HTTP-supported client app/utility, the REST Client allows you to send HTTP requests and view the response in Visual Studio Code directly.
- Open the prepared API docs located at:
docs/api/index.http
- Click the
Send Request
link above the request OR use the shortcutCtrl+Alt+R
(Cmd+Alt+R
for macOS). Read more about usage on REST Client extension's homepage. /login
endpoint is already configured to use the dummy user credentials. On successful response, it returns a JWT Access Token and set the variable@authToken
(used to browse secured endpoints).- Endpoints performing CRUD operations require a JWT Bearer token for authentication. And already configured to make use of the request variable
@authToken
. - To browse a single resource, copy the
id
from thePOST /users/
response and replace the path parameter<user.id>
with it.
Note: This codebase is just for demonstration purposes. Any authenticated user is able to change the email/password of any other user in the database. That won't be the case in a real scenario.
Once done, stop the Docker containers:
docker compose down
<OR>
docker-compose down