Who's never worked on a spaghetti code? Luck you.
I've worked on sever projects that had some spaghetti code. And all this started because of a lack of structure.
A well-structured application helps you avoid that mess by clearly separating concerns. One common and effective approach is using a layered architecture.
While the number of layers can vary depending on your domain, the core idea is the same: group logic by responsibility. Here's how I typically split it.