Search code examples
htmlrestapirestful-architecture

How to serve static (or dynamic?) HTML files with RESTful API?


Hi I'm studying about RESTful API and making a website running on local to exercise.

I think RESTful is a quite good way. CRUD operations can be identified by HTTP methods and we can handle them with one url.

But most confusing things to me is that, How can we serve HTML files which are needed to request CRUD operations?

For example, If I'm implementing a forum, I need APIs to CRUD posts in forum like


[GET] /forum - see all posts in forum
[POST] /forum - create a new post
[GET] /forum/:id - see the post of id
[PUT] /forum/:id - modify the post of id
[DELETE] /forum/:id - delete the post of id

Think about how we use a forum, we need at least 3 type of HTML pages.
They are,
1. a page to see all posts in forum.
2. a page to see the specific post.
3. a page to type title and contents to create(or modify) a new post.

First and second type of HTML files can be served easily by GET requests above.
But in case of third type HTML files, I need to use extra parameters with above APIs or make a new API such like /forum/createpost to serve such HTML files.

I think, in the point of view of RESTful, I miss something and need to distinguish serving static (or dynamic) HTMLs and handling CRUD requests.

What is the bestpractices to handle this problem?
I also find some questions about this problem, but I couldn't find a clear answer.


Solution

  • I think you are mixing up two separate parts of the application. One is the REST API that provides the endpoints for CRUD operations. The HTML files that send the API requests are not part of the REST API. They are served by a web application that provides the front-end to the user, and makes calls to the REST API in the backend to fetch the information to display. To put it in another way, the web application making the calls is your Presentation layer. The REST API is your Business logic. Presumably, the REST API interacts with a database to write data to and read data from it. That is your Persistence or Storage layer.