Friday, 2 June 2017

When building a REST API with node, should I have some sort of application between my REST API and my DB?

Hey /r/node! I am relatively new to node and have been struggling for a few days trying to build a relatively simply REST API to go with a website. I managed to build a prototype I'm happy with to get more comfortable with node, but I am trying to take that and make it a testable and extendable project. This is where I've hit a wall.I come from a Java background, so I initially tried to take an Object-Oriented approach, where I have express handling REST endpoints, which connect to the actual application, which handles users and other data, and writes to the (also separate) database. Unfortunately, I am REALLY having trouble creating the middle step.After some research, it seems that javascript is actually more imperative than anything else. Is looking at this as an OOP problem making my life more difficult? Is it okay to just have my REST endpoints interact with the database directly? (I'm assuming this can't be right. It's hardly testable and it's near impossible to make any sort of changes smoothly.)I would really appreciate any direction anyone can give me, even if there's just a decent example of a relatively large-scale node application that features a REST API and some sort of DB. Even better, is there any particular design pattern for doing this kind of thing in an imperative language? Thanks in advance!** EDIT **Just to give a more clear example, let's say I'm making a blog site. I have my REST API where users can sign up, log in, submit posts, and view posts. I store these users and posts in a database. When a user submits a post, do I write my express routes to just write that post to the database, or query the database and return the result? Or, should a post submission go through my myBlog module (Is that the correct term?), which handles validation, creating the post, and writing it to the database?

Submitted June 02, 2017 at 04:17PM by bits_bytes_bots

No comments:

Post a Comment