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:
appis the application: its components.sysis what Toa serves beside every application. The gateway, which answers HTTP on port8000, 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
| Command | Does |
|---|---|
npm run restart | Generates again and restarts both processes. Run it after any change. |
npm run features | Runs the scenarios. |
npm stop | Stops both processes. |
pm2 logs | Shows what the processes write. |
pm2 ls | Shows whether they run. |
docker compose down | Stops 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:
| File | Is | Explained in |
|---|---|---|
components/hello | A component with code and no state. | Run It and Make a Change |
components/notes | A component with state and no code. | Run It and Make a Change |
features | The scenarios, and the steps they are written with. | Write a Test |
context.toa.yaml | The Context: the application’s name, where its infrastructure is, its settings. | Add Configuration, Describe the Application |
ecosystem.config.js | The two processes PM2 keeps. | Run and Configure the Gateway |
docker-compose.yaml | RabbitMQ, MongoDB and Redis for a local run. | Deploy |
package.json | The packages of Toa and the scripts above. | The CLI |
tsconfig.json | What npm run typecheck checks the sources with. | Use TypeScript |