This post was originally published on my blog.
When I started using Node.js for building REST APIs on the server side, I struggled a lot with t...
For further actions, you may consider blocking this person and/or reporting abuse
What's the reason of repeating the name of the module in the modules directory? For example you got
modules/user/user.controller.ts
, how about justmodules/user/controller
? I don't see the point of duplicating it. When you import it, it looks weirdimport UserController from './modules/user/user.controller'
Same question applies to the services. I don't like the idea of adding ".service" to the file names. I already know that I am in the services folder.
Anyway, I like to modular approach!
Yeah, that's actually a good point. Thanks!
One advantage might be: It's easier to differentiate the files by the tab titles in your editor :)
I recently started on a little TypeScript Express starter project and I'm enjoying it so far. I'm also looking to add TypeORM to my setup! I'm trying to create a super lean setup to replace Adonis.js as the maintainer has refused to adopt TS.
I was hoping you could answer some questions though.
I was trying to find a convenient way that to add all the routes and I struggled with that (though I just started learning TS). Looking at your setup, does each module router create a new Express router? And those are then exported and added to the Express instance via the
app.use('route group', module_router)
?? I never knew it accepted instances of a router.Yeah, each router in the /modules/ folder creates a new instance of the express router and those instances are exported to server.ts
This might be interesting for you: expressjs.com/en/4x/api.html#router
Thank you for the nice post. I am beginner to Expressjs and have some beginner doubts :-
1) Why should we have "services" seperately (why not write the logic in controllers themselves?)
2) If controllers are to be kept seperate from services for some reason, how are routes and controllers different (since the controllers and routes are accepting the same parameters and the controller expects results from service, why can't a route directly call service instead and respond to the request?)?
I hope that I am missing some case where each of them is to be seperate, but can't get it.
1)
If we have the services (logic) separated from the controllers, we can query the required data from anywhere else in our codebase. Otherwise you have to write the same code every time again just to get the same data. (or you send a request to the API itself -> bad)
2)
A route does not directly call a service, because before calling the service the controller takes care of the incoming HTTP request, validation and the outgoing response. In a controller we might send different HTTP status codes or prepare the data before sending it back to the client. In a service we just load the data and handle the business logic. It does not care about where it gets called from.
Let's say you call a service, that expects parameters, directly from a route. Now the router sends the incoming HTTP request as argument to your service. But what if you want to call the service from within your codebase and not from an incoming HTTP request? You can't really pass a HTTP request as argument.
Thank you. Everything is clarified now.
Interesting post :)
I'm messing with Go at the moment, and the structure is definitely one of the key factor.
Yep, that's the dodgy one. I'd be very interested in knowing how you handle it though.
Have a look here :)
dev.to/larswaechter/unit-testing-f...
Do you have some example code for a simple (one or two component) API? I'm wondering whether each model contains a database connection with SQL operations etc, object or if it is just a model representing each table.
This is helpful though, thanks.
How did tdo the file structure thingie, is it markup or an image?
It's markdown: stackoverflow.com/a/24961998
Thanks! I couldn't select the text on my mobile browser and I thought...Anyway I installed
tree
to generate it for mePretty nice explanation. So sad your web app is not accessible because of HSTS!
Thanks :) The web app is just a landing page for one of my projects. I'll fix it soon!
I think you should mention earlier that you're using Express. 🙃
And maybe if your setup is valid for alternatives, e.g. Fastify or Hapi.
I added Express and TypeORM at the beginning :)
Note: I just refactored the folder structure a little bit :)