Search code examples
node.jsexpressfrontendbackendserver-side

What type of operations should be performed in the backend?


So I understand the difference between backend and frontend, but what type of operations should I just leave for the backend. For example, if I'm using Node.js with Express, how should I decide what operations/calculations I should do on the client side or the server side?

Thanks in advance!


Solution

  • First off there's a set of things you pretty much have to do in the back-end:

    1. Persistent storage on behalf of a user that will work from any browser. This would include their account, any state/settings on behalf of that user and any data that for that user.

    2. Any algorithms or code that needs to remain a trade secret. All client code is visible to anyone. Server code can be kept private.

    3. Some security operations.

    4. Things that a client cannot necessarily do such as contact any arbitrary external host to fetch some data.

    5. Validate all data sent from the client before storing it or operating on it. Clients cannot be trusted so all incoming data much be checked/validated.

    6. Create accounts and validate user logins.

    Then, there are things that are generally easier to do in the client:

    1. Handle simple user interactions such as prompting for configuration before carry out some irreversible action (perhaps deleting some data).

    2. Give the user feedback as they proceed on some task (such as filling in a form or auto-completing some entry).

    Then, there are things that can be done in either place and whether it is more or less appropriate to put them on the server or the client really depends upon your specific application, your general architecture, etc... And, they may even be done in both places.

    1. Render HTML templates. Can be done in either back-end or front-end and may actually be done in front-end for normal user viewing and in back-end for search engine viewing.

    2. Data validation and user feedback.

    3. Send an Ajax call from the client to the server to process some action and then update the view in the client to show the results of that action without reloading the page. For example, deleting an email from Gmail. This is a combination of front-end and back-end code to implement one operation.

    how should I decide what operations/calculations I should do on the client side or the server side?

    That's a very difficult thing to answer in the abstract. As I said above, some things have to be on the server or client, but others can be done a variety of ways. It would be better if you described your application and then listed 5 different capabilities in your application you were trying to decide where to implement them. Then, we could answer about specific capabilities in the context of your app and design.