Roman Mahotskyi
Roman Mahotskyi

Reputation: 6625

How to organize services in express

I am trying to configure the express server and organize project structure within it. I read a lot of article of how to create "bullet proof" structure of the project.

Currently I am stuck on the service layer and don't really know how to organize services to have them scalable in the future.

Let me show what I mean

I expect to have authentication in my app by using session cookies. Also each user will have their profile and possibility to change any information of their profile, including password, email etc.

Currently I don't know which information is part of which service.

Should it be like (endpoint -> service)

/auth/login -> AuthService
/auth/logout -> AuthService
/auth/register -> AuthService

/session/terminate-specific-session -> SessionService
/session/terminate-all-sessions -> SessionService
/session/terminal-all-session-expect-this-one -> SessionService

/user/change-password -> UserService

or all of this stuff belongs to the AuthService. Should I split session & authentication, or it should be inside single service. Or I have to create AccountService which will be responsible for handling all account stuff or not... I literately messed up

I would like to know your thoughts of how to organize services and their responsibility

Big thanks

Upvotes: 1

Views: 294

Answers (1)

jfriend00
jfriend00

Reputation: 707308

First off, don't overthink this or over design it at this point. You should spend some serious thinking about your URL design and how exactly you want the long term nature of your URLs to be because that is visible to the outside world, can be involved in a lot of HTML pages and user bookmarks and all that and can be a pain to change.

But, your internal file layout is no big deal if you grow a bit in the future and want to change the layout. In less than 5 minutes, you can create a new router, break 3-5 routes out of a previous file and hook in the new router. So, changes like that in the future are easy and simple to make.

Second, I'm going to assume by "service", you mean router or group of routes on a router since router is the term that Express uses (Express doesn't use the term service at all).

Third, I think a project is actually more complicated than needs be if things are initially put into a zillion tiny files with a zillion small routers. The URL design needs to plan for the future, but the file layout does not have to anticipate every possible change in the future.

Logically, it seems like the /auth routes and the /session routes are all really about authentication. If you only had the few routes you show, I'd organize them into one file since they may share some common code and common imports and probably use a /auth router and a /session router within that file.

The /user route seems like it's likely to have other routes related to is (changing user preferences and things like that) so I'd probably put it in it's own file and router and can then add other /user routes to it going forward.

Upvotes: 2

Related Questions