Hi Node Community,I've been living in an Objective-C world for a very long time so please bear with me. I am building a pretty standard Express web application.Here are some use cases to describe my situation:Use case 1 - User wants to visited the blog on a website.Request is received. app.get('/blog', blog.fetchPostsAndRenderView);fetchPostsAndRenderViewmakes a call to the DBOnce calls to the DB complete, data is passes to view templateView is rendered into HTML and returned to the browser.Use case 2 - Site admin wants to add a blog postRequest is received for HTML form to create blog post app.get('/admin/blog/add`, checkAuth, blog.renderAddPost);Form is directly returned to site adminSite admin fills in blog post details and submits post to be savedBrowser makes an AJAX POST request to /api/blog/Request to create post is received app.post(/api/blog, blog.createPost);Blog module writes post to DB200 status returned.Basically what this boils down to is that I have REST API end points to create, update and delete blog posts. These API end points just use the blog module to do their respective operation. Yet when I need to retrieve a specific post or all of them, I run a query on the DB directly instead of using an API end point.I find it strange, muddy and just off that when a request is received to view the blog page, I hit the DB directly on the server and pass the view the data. Yet when a user creates a blog post, I use an API end point to create it.Is it common to have a half completed REST API that is used to create, update and delete things like this?I feel as if I should have an API end point that returns the blog post data needed in Use Case 1. When the request comes in for "/blog", it uses the "/api/blog/" end point to fetch all posts before rendering the view. Is this possible/best practice?I also understand that I could actually return nothing when the user visits the blog page and AJAX to an API end point to get all posts. However, my feelings are if the server can render it initially, why not? Caching, performance, etc. I think I want to stick with server-side rendering, but I feel as if I should use a formal REST API to fetch the data needed to render the views.
Submitted March 07, 2016 at 01:47AM by programming_owl
No comments:
Post a Comment