Node.js Getting Started Tutorial Part V (Express framework)

Express framework

Express is suitable for Node.js web framework that provides a number of useful features, such as routing and http function.

Express framework core features:

  • Middleware may be provided to respond to HTTP requests.
  • It defines the routing tables for performing different HTTP request operation.
  • Can be dynamically rendered by passing parameters to the template HTML page.

 

installation:

 npm install express --save 

 

You may require middleware:

Parser-body  - N ode.js middleware for processing JSON, Raw, Text and URL -encoded data.

Parser-the cookie  - this is a parsing Cookie tools. By req.cookies can take to pass over a Cookie , and turn them into objects.

multer  - Node.js middleware for processing the enctype = "multipart / form-Data" (set form MIME encoding) data form.

CORS - Node.js cross-domain solution using cross-domain access when needed.

1 npm install body-parser --save
2 npm install cookie-parser --save
3 npm install multer --save
4 npm install cors --save

Use express to create a server:

. 1  var Express = the require ( 'Express' );
 2  var App = Express ();
 . 3   // distribution route 
. 4 app.get ( '/', function (REQ, RES) {
 . 5     res.send ( 'the Hello World' ) ;
 . 6  })
 . 7 app.get ( '/ About', function (REQ, RES) {
 . 8     res.send ( 'About Web' );
 . 9  })
 10 app.post ( '/ User', function (REQ, RES ) {
 . 11     res.send ( 'User Data' );
 12 is  })
 13 is  // Create a server and listen on port 8080
14 var server = app.listen(8080)

Access  http://127.0.0.1:8080 :

Interface output 'Hello World'

Access  http://127.0.0.1:8080/about :

Interface output 'about web'

Access  http://127.0.0.1:8080/user :

Interface output 'user data'

 Express routes into get and post two kinds. Both use similar, but post greater support parameter length.

 

express + axios achieve vue front and rear side cross-domain access (to expand)

axios is ajax module packaged, easier to use, can be express with the use of separate front and rear ends to achieve cross-domain access.

Installation Axios :

 npm install axios --save 

 Use express create a route:

1 //router.js
2 const express = require('express');
3 const router = express.Router();
4 
5 router.get('/login', (req, res, next) => {
6   //to do login
7 });

Create a cross-domain access:

. 1 const = routerApi the require ( './ Router' );
 2 const = bodyParser the require ( 'body-Parser'); // POST data needs 
. 3 const = Express the require ( 'Express' );
 . 4 const = CORS the require (' CORS '); // domain access middleware dependent 
. 5 const = App Express ();
 . 6  
. 7  // allow the requested domain, if * is present to allow all the server domain names (written must be registered before any routing) 
8 app.use (CORS ({Origin: 'http://127.0.0.1:8080' }));
 . 9  // parse Json 
10  app.use (bodyParser.json ());
 . 11  // registered route 
12 is app.use ( '/ API' , routerApi);
 13 is // create the service side, listening port 
14 app.listen (3000, '0.0.0.0' );
 15 console.log ( 'the listen Success AT Port: 3000 ......');

Front-end main.js (front end Vue for example) :

. 1 Import from Vue 'VUE'
 2 Import from Axios' Axios'
 . 3  
. 4  // use the PC ElementUI front-end frame 
. 5  Vue.use (ElementUI)
 . 6  // server requests the Url 
. 7 axios.defaults.baseURL = 'HTTP: // 127.0.0.1:3000/ ' ;
 . 8  // set the default type of post JSON 
. 9 axios.defaults.headers.post [' the type-the Content '] =' file application / JSON ' ;
 10 Vue.prototype.axios = Axios

Distal UI request:

 1 this.axios.get("/api/login", {
 2    params: {
 3      userName: 'Jimmy',
 4      password: '123456'
 5    }
 6  })
 7  .then(res => {
 8   //登录结果...
 9  })
10  .catch(error => {
11    console.log(error.response);
12  });

 

Guess you like

Origin www.cnblogs.com/JHelius/p/11645929.html