express request routing structures rear end, the rear end of the front end interface to access the corresponding cross-domain solution

Code == "E: \ nodes combat \ myserve \ testserve

. 1 Express request routing to build the rear end, the front end access interface corresponding to the


    1 ) Create a project directory
        express project name - E

        Then follow the prompts on it
        cd project name


    2 ) into the project relies download
        cnpm i 


        Appear during the download process
        New minor version of npm available! 6.11.2 -> 6.13.1      
        Changelog: https://github.com/npm/cli/releases/tag/v6.13.1   
        Run npm install -g npm to update! (运行npm install-g npm进行更新!)
        With the new on it
        

    
    3) In (module.exports = app.js in the app; is added before addition) at line 40 follows monitoring port
        app.listen(666, () => {
            console.log ( 'back-end server is started successfully, the address is: http://127.0.0.1:666' )
        })


    4 ) to start the project
        nodemon app

Then enter HTTP: // 127.0.0.1:666 - will appear interface

 

Tell us about the back end of the project directory
bin
public
routes routing interface
views
app.js file
package.json package description file


Copy the file to a users.js routes, it is then used to make requests account name file follows account.js
{
    var express = require('express');
    var router = express.Router();

    // unified set of response headers to solve cross-domain problems 
    router.all ( "*", (REQ, RES, the Next) => {
         // set response headers to solve cross-domain (the most mainstream way) 
        res.header ( "Access- the Allow-Origin-Control "," * " );
        res.header("Access-Control-Allow-Headers", "authorization");
        next();
    });

    /* GET users listing. */
    router.get('/', function(req, res, next) {
    res.send ( 'solve cross-domain Ha' );
    });

    module.exports = router;

}


Then go to app.js / test allocate a route
= accountRouter the require Vvar ( './ routes / Account');   // allocate routing the line 10 
app.use ( '/ Account', accountRouter);       // use the routing, line 28


Then enter the page http: // 127.0.0.1:666/account 
appears to solve the cross-domain route is configured Kazakhstan Description

 

Distal
 created () {
    axios
      .get("http://127.0.0.1:666/account")
      .then(res => {
        this.mesg = res.data;
        window.console.log(res.data);//解决了跨域哈
      })
      .catch(err => {
        window.console.log(err);
      });
  }

 

 

Guess you like

Origin www.cnblogs.com/IwishIcould/p/11925154.html