TypeScript with Express
Cover Page
Back-end Page
handlers.ts
Change to your chatterd folder and edit the file handlers.ts:
server$ cd ~/reactive/chatterd
server$ vi handlers.ts
Add one new property to the Chatt type, as the last property in the list:
type Chatt {
// . . .
audio: string | null
}
To the back-end database, audio is just a string. Since the audio string optional, we allow its value to be null.
To handle audio data uploads, make a copy of your postchatt() function inside your handlers.ts
file and name the copy postaudio(). Replace the call to chatterDB\INSERT` with:
await chatterDB`INSERT INTO chatts (name, message, id, audio) VALUES (${chatt.name}, ${chatt.message}, ${randomUUID()}, ${chatt.audio})`
which extracts the audio entry from the JSON object and insert it into the chatts table,
along with the rest of its associated chatt.
Next, make a copy of your getchatts() function inside your handlers.ts file and name the copy
getaudio(). Replace the SELECT statement with: SELECT name, message, id, time, audio FROM
chatts ORDER BY time ASC. This will retrieve all data, including our new audio string from the
PostgreSQL database. In addition to the original columns, we added reading the audio column and
included it in the chatt data returned to the front end.
We’re done with handlers.ts. Save and exit the file.
main.ts
Edit the file main.ts:
server$ vi main.ts
Find the initialization of app and add the following new routes for
the new APIs /getaudio and /postaudio:
.get('/getaudio/', handlers.getaudio)
.post('/postaudio/', handlers.postaudio)
We’re done with main.ts. Save and exit the file.
Build and test run
To build your server, transpile TypeScript into JavaScript:
server$ npx tsgo
![]()
TypeScript is a compiled language, like C/C++ and unlike JavaScript and Python, which are an interpreted languages. This means you must run npx tsgo each and every time you made changes to your code, for the changes to show up when you run node.
To run your server:
server$ sudo node main.js
# Hit ^C to end the test
Unfortunately there’s no practical way to include a base64-encoded sample audio string,
nor a way to play it back without a front-end to decode it. The simplest way to test
is to use your front end to record an audio message and post it to your back end, and
then retrieve the chatt from the back end for play back.
| Prepared by Sugih Jamin | Last updated August 11th, 2025 |