7

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.

5
  • Look into routing, you're better off using a framework for routing and your API for handling requests. Which stack are you working in?
    – Adrian
    Aug 2, 2017 at 7:32
  • @Adriani6 what you mean stack? okay. sorry i forgot. I am using nodejs and express. not using template engines and using pure HTML5 and javascript, no jQuery or Ajax. Aug 2, 2017 at 7:38
  • 1
    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. May 29, 2018 at 21:25
  • @AjoyBhatia you just clean up all my concerns. That gives me a very clear view of how to build up. Thanks! Jun 8, 2018 at 9:16
  • Glad to have been of help. :-) Looking at the answers, I think they miss the mark, and I will post my comment above as an answer. Jun 8, 2018 at 21:16

3 Answers 3

11

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.

3

You can use HTTP content type negotiation for that. POST/PUT requests (can) contain a Content-Type header declaring the type of content they're sending, and—more importantly—all requests contain an Accept header declaring the kinds of responses it accepts. If the client is accepting text/html responses, serve an HTML page; if they're accepting, say, application/json responses, serve a "RESTful" JSON response. This way your server can respond to different situations with the appropriate content and the same endpoint can serve as API and as HTML handler.

Alternatively, you can distinguish the request by using an extension: /posts.html serves a plain HTML file, while /posts gets served by a REST endpoint. That can easily be done in the web server configuration.

1
  • So you mean that we can send requests with same method and url but different 'Content-type'. Server can distinguish them by 'Content-type' and can operate or reply differently. Thanks for your reply. Aug 2, 2017 at 7:50
1

This might or might not be an anwser to your problem, however since you're working in Node + Express, routing might be a way to go (If I understood your question correctly). Below is an example of server implementation of accepted routes with parameters. Note, you can make parameters optional in some cases if needed.

app.get('/', function (req, res) {
  res.send('Index')
})

app.get('/forum', function (req, res) {
  res.send('Forum Index')
})

app.get('/forum/:id', function (req, res) {
  // To access id you do 'req.params.id'
  res.send('Forum Index')
})

app.put('/forum/:id', function (req, res) {
  res.send('Modify Forum')
})

app.delete('/forum/:id', function (req, res) {
  res.send('Delete Forum')
})

Reference : https://expressjs.com/en/guide/routing.html

1
  • Thanks for your reply. :- ) I already implemented CRUD APIs like your answers! I need to way to serve HTML files that send API requests. Thanks! Aug 2, 2017 at 7:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.