This is an example skeleton application that provides a simple implementation of:
- Koa web framework with Typescript
- PostgreSQL database via Docker, connected with TypeORM
- Local authentication with Passport
- User registration/login with salted & hashed passwords via bcryptjs
- User sessions with cookies, provided by koa-session
- Request object schema validation with Joi
- Node.js version 8+
- Docker with docker-compose
In the repository root directory (where docker-compose.yaml exists), run: docker-compose up -d
This will create the database container from the official postgres Dockerhub image and configure a test database.
Once created, the environment can be controlled with docker-compose start and docker-compose stop. To entirely remove it, run docker-compose down.
First, run npm i to install dependencies.
Then, two run scripts are provided:
-
npm startwill transpile the Typescript files in/srcone time into/dist, then serve it locally with nodemon. -
npm run devwill run the Typescript compiler in watch mode concurrently with nodemon in watch mode, recompiling changes on the fly and restarting the node service.
POST to /register:
{ username: 'foo', password: 'bar', firstName: 'Test', lastName: 'User' }
Creates a new user, logs in, and returns a session cookie
POST to /login:
{ username: 'foo', password: 'bar' }
If successful, logs in and returns a session cookie
To test that your session is working, GET the protected endpoint /api/users
[
{
"id": 1,
"username": "foo",
"firstName": "Test",
"lastName": "User"
}
]
This is just a starting point example, and is not production-ready.