Search code examples
restweb-applicationsarchitecturebackendapi-design

Application route serve html only? Should application route update state?


I am wondering what the best practice is for scenerios like serving a signup page to create new users. There are two separate things going on here.. 1) application must forward a user creation html page and 2) user creation forum must be passed to an endpoint for user creation.

Should you combine these two processes as part of your application logic? Or should user creation be handled by a separate API and application would only serve html?

Option #1:

// Application routes serving HTML for GET and updating user state for POST
@app.route('/signup', methods=['GET', 'POST'])
                   return html--^    ^---- create new user

Option #2:

// Application routes serving HTML for GET:  
@app.route('/signup', methods=['GET'])
  
// Seperate REST API endpoint for user creation:  
@app.route('/api/user/', method=['POST'])

Solution

  • You should separate HTML serving from backend services that actually form your backend API. These are two separate resources, with specific purposes, and you should not mix and match them like that in a single endpoint.

    This will also make it easier for you to reuse the backend endpoints, as it makes a lot more sense to invoke an API on a URI /api/user from another system, to create a resource, than it would make to do it over a URI /signup, which only makes sense in the context of the app you're creating.

    I'm not very familiar with flask or python, but there are several tutorials that focus on creating RESTful using both, such as this one: https://blog.miguelgrinberg.com/post/designing-a-restful-api-with-python-and-flask

    Don't forget to use jsonify to set up your API response if you need to return any JSON data.