How to separate the front and back ends

 Clear responsibilities :

  • Front-end work: implement the entire front-end page and interaction logic, and use ajax to interact with the nodejs server (middle layer)
  • Back-end work: Provide API interface, use redis to manage session, interact with database

Steps :  

        1 Front-end deployment: If you write a static html page and open it directly in the file directory, you cannot make ajax requests (browser cross-domain restrictions). Therefore, you need to run a server locally and use the express framework of nodejs to Start a local server, and then use an http-proxy-middleware plug-in of nodejs to forward the request sent by the client to nodejs to the real server, so that nodejs acts as a middle layer. In this way, the front end can be developed without hassle

       2 Set the API and simulate the data: After the front-end and back-end are separated, when the front-end and the back-end are developed at the same time, you may encounter the situation that the front-end has already developed a page, but is waiting for the back-end API interface. You can use the mock (http ://mockjs.com) to return some fake data.

   nodejs front-end server

         Select nodejs:

        Nodejs itself has the unique characteristics of asynchronous and non-blocking I/O, which means that it is particularly suitable for I/O-intensive operations and has a strong ability to handle requests with a large amount of concurrency. Therefore, it is used as a front end. A server, serving static files to clients and responding to client requests, is a good choice.

       deploy:
 

  1. Pack the developed front-end code into static compressed files using webpack
  2. On the server, use the pm2 load balancer to execute the following code to start the server:

      

  Supplement [Permission Control]

        Control is all handed over to background processing.
        When logging in for the first time, the user information is generated and tokenreturned to the client.
tokenThe security of the data is handled by the background, and the data obtained by the front end is considered to be correct, and the permissions are not controlled.
Every time the front-end requests the back-end interface, it will be tokenbrought in. tokenThe correctness is verified by the background , and the corresponding result is returned.
Just use standard restful api.

     From the point of view of data security, the front-end can not use permission control at all; if you can't do this there, then there must be a problem with your back-end permission control.

    The front-end permission control is only to give users a better physical examination; it is not really used to control permissions

Reference: http://blog.jobbole.com/111624/

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325344284&siteId=291194637