Introduction
ExpressoTS is a new developer-friendly TypeScript framework for Server-Side Applications. It’s currently built on top of Express, easy to get a new developer on board, light and easy project structure, two different project structure supported (opinionated and non opinionated version), supports MVC, non MVC, architecture. The opinionated template was built on top of clean architecture, you will see concepts embedded in the opinionated template such as entities, useCases, repositories and providers.
The logo correctly indicates the speed of the framework.
Setting Up Your First ExpressoTS Project
Ah, the first step to any great journey in the world of coding—a proper setup. Trust me, nobody wants to get lost before even starting the adventure. So, let’s cut the chit-chat and dive right in.
Installation of ExpressoTS CLI
You’ve got two routes here, and the destination is the same, a functional ExpressoTS environment. Your first option is to install the ExpressoTS CLI globally:
Got limited commitment issues? No worries, you can also use dlx
to run the CLI without installing it globally. Ah, the wonders of a one-night stand with a CLI!
Configuration Wizard
After running the command, you’re greeted by a friendly (albeit text-based) wizard 🧙♂️.
Fill out the form wisely but don’t overthink it. For this tutorial, I’m going with the “Non-Opinionated” template because, it will give us a shallower learning curve in the start.
Getting Comfy in Your New ExpressoTS Home 🏡
Navigating to Your Project
So you’ve set up your new ExpressoTS project. Awesome! Time to get inside the engine room and take a look under the hood.
You’re now in the root folder, and if you’re curious about what comes packed by default, run a quick tree
command.
There you go! These are the files you’ll be living with.
Customizing Your Prettier Preferences
You’ve got your own coding style—don’t we all? You can tweak the .prettierrc
file to your heart’s content.
Let’s Take a Quick Tour of package.json
A glance at the scripts
section in package.json
tells you all you need to know to get things up and running.
Here, you’ve got your usual suspects: build, dev, test, and more.
Format All The Things
Before diving into development, let’s keep it neat. The format
script will handle that.
Running in Dev Mode
And finally, the moment you’ve been waiting for—fire up the dev server and see your app come to life!
Making Your First Request in ExpressoTS 🌐
Time for a Little Hello
You’ve been through the house tour, you’ve feng shui’d your .prettierrc
and got your scripts all figured out. Now what? Well, how about actually seeing your app in action?
Using HTTPie for Your First GET Request
For this demonstration, we’re going to use HTTPie — the cURL
for the 21st century. But like, way more readable. Here’s how to send your first GET request to http://localhost:3000
.
Bam! You should see a response like:
Congrats! You just made your server say hello.
Anatomy of the “Hello Expresso TS!” 🦴
Alright, let’s dissect that "Hello Expresso TS!"
message. How does this simple string pass through layers of TypeScript files to make it to your browser? Spoiler: It’s not magic; it’s just well-structured code.
The Starting Point: src/main.ts
This is where the baton is picked up. Here, we import the essential parts from ExpressoTS and set up the initial application instance. Simple enough.
The Container: src/app.container.ts
Think of this as the backstage, where everyone gets ready for the show. It’s where the application container is configured with your custom modules.
The Module: src/app.module.ts
Modules in ExpressoTS group related functionalities. Here, it’s as simple as importing the AppController
.
The Controller: src/app.controller.ts
This is the conductor of our orchestra. It’s responsible for handling HTTP requests and directing traffic. In this case, it’s just saying, "Hello Expresso TS!"
.
The Use Case: src/app.usecase.ts
Here’s where the actual "Hello Expresso TS!"
lives.
”Hello, Who?” Changing the Default Response 🖊️
Got bored of the usual "Hello Expresso TS!"
? Let’s give it a personal touch. To do that, we only need to venture into the src/app.usecase.ts
file. See, this is the beauty of a well-structured codebase; you don’t have to jump through hoops to make a simple change.
The Tweak: src/app.usecase.ts
One line change. That’s it. Here we just swap out the text to "Hello from <Daniel::Boll>"
.
The Reload: Auto-refresh FTW
As soon as you save that change, the development server detects this update and reloads itself. No manual effort required. Ah, the joys of modern development.
The Result: Let’s Talk to the Server Again
Run the command, and voila! The updated greeting is now served hot, right from your server.
Tuning Port via Environment Variables 🎛️
Great, we’re diving into environment-specific configuration! This makes the application flexible and easier to deploy in various environments.
Port Configuration in src/main.ts
We modify the AppInstance.listen()
method to get the port from the environment variable PORT
. If PORT
is not available, it defaults to 3000
.
Environment Checks
Adding Environments.checkAll();
ensures that our environment is sound before the application starts. This is a proactive approach, helping to catch issues early.
Feedback on Missing .env
and Variables
When you don’t provide the .env
file or the PORT
variable, ExpressoTS kindly informs you. This is immensely helpful for debugging and avoids “silent failures.”
Creating .env
and Updating Port
- We start by creating an empty
.env
file, simulating a missingPORT
variable. - The server informs us that the
PORT
variable is missing. - We then set
PORT
to3001
in the.env
file.
Verify New Port
Finally, when the server restarts, it picks up the PORT
value from .env
. As expected, the server now runs on port 3001
.
Fantastic, we now have a flexible, environment-aware application. This is a crucial step for making the application ready for various stages of deployment, like development, testing, and production. 🌐
Utilizing the CLI for Efficient Development 🛠️
Changing to Opinionated Mode
Switching the project to be opinionated is as simple as modifying expressots.config.ts
:
Scaffolding Services with the CLI
The Expresso CLI is versatile and can assist in creating various resources. To understand its capabilities:
For our scenario, we’ll generate a service named user/create
, which will scaffold a controller, use case, and provider.
This automates a plethora of tasks:
- Creating necessary files
- Adding the new module to
app.container.ts
- Extending the existing
app.module.ts
The changes are reflected in src/app.container.ts
:
Structuring the Service Directory
By default, without a trailing slash in the CLI command, the scaffold looks like this:
However, adding a trailing slash creates a more modular folder structure, better suited for extending functionalities such as CRUD operations.
After reverting previous changes:
The new structure is:
This fine-tuned control over the codebase structure is immensely helpful for managing large projects efficiently.
Completing the User Creation Feature 🛠️
Updating DTOs
Your DTOs specify the shape of incoming and outgoing data:
Adjusting the Controller
The controller now listens to POST requests and forwards the payload to the use case:
Updating the Use Case
The use case is now a singleton, maintaining a state of user records:
Testing the Functionality
First attempt at creating a user:
Second attempt fails as expected:
Wrapping Up 🎉
Although this approach may not align 100% with best practices, it provides a functional example. The Opinionated Architecture extends this example into a more robust setup, which includes repositories, entities, and more.
Thanks for following along! Hope this was insightful and stay tuned for more! 👋