I First Steps

Create an Application

A small shop takes orders. Its application does not start from an empty file: one command writes an application that already runs, and the shop is built by changing it.

§What the machine needs

  • Node.js 24 or later.
  • Docker, for the broker, the database and Redis the application connects to.
  • PM2, which keeps the application’s processes running:
$ npm install -g pm2

§Create it

$ mkdir store && cd store
$ npx -p @toa.io/cli -p @toa.io/userland toa create store

The directory has to be empty; .git may be there. store is the application’s name.

§Start it

$ npm install    # the packages
$ npm run dock   # the broker, the database and Redis, in Docker
$ npm run env    # what is generated from the sources: types, .env and .map.json
$ npm start      # the application
┌────┬────────┬─────────┬────────┬───────────┐
│ id │ name   │ mode    │ uptime │ status    │
├────┼────────┼─────────┼────────┼───────────┤
│ 1  │ app    │ fork    │ 3s     │ online    │
│ 0  │ sys    │ fork    │ 3s     │ online    │
└────┴────────┴─────────┴────────┴───────────┘

Two processes are running:

  • app is the application: its components.
  • sys is what Toa serves beside every application. The gateway, which answers HTTP on port 8000, is part of it.

They are separate here because they are separate in a deployment.

§Send a request

$ curl 'http://localhost:8000/hello/?name=Toa'
{"greeting":"Hello, Toa!"}

The application came with two components. hello answered this request; notes keeps notes:

$ curl http://localhost:8000/notes/ 
    -H 'content-type: application/json' 
    -d '{"text":"Buy milk"}'
{"id":"01a115eaddd7717ba992e9bc4d95b29c","text":"Buy milk"}
$ curl http://localhost:8000/notes/
[{"id":"01a115eaddd7717ba992e9bc4d95b29c","text":"Buy milk"}]

§Run its tests

$ npm run features
2 scenarios (2 passed)
8 steps (8 passed)

§Day to day

CommandDoes
npm run restartGenerates again and restarts both processes. Run it after any change.
npm run featuresRuns the scenarios.
npm stopStops both processes.
pm2 logsShows what the processes write.
pm2 lsShows whether they run.
docker compose downStops the broker, the database and Redis.

§What was created

Nothing here needs reading before the next article. Each file is explained where the shop first changes it:

FileIsExplained in
components/helloA component with code and no state.Run It and Make a Change
components/notesA component with state and no code.Run It and Make a Change
featuresThe scenarios, and the steps they are written with.Write a Test
context.toa.yamlThe Context: the application’s name, where its infrastructure is, its settings.Add Configuration, Describe the Application
ecosystem.config.jsThe two processes PM2 keeps.Run and Configure the Gateway
docker-compose.yamlRabbitMQ, MongoDB and Redis for a local run.Deploy
package.jsonThe packages of Toa and the scripts above.The CLI
tsconfig.jsonWhat npm run typecheck checks the sources with.Use TypeScript
Search
Search the documentation