Use node + Express + Http-proxy-middleware to implement a detailed instance of the front end agent cross-domain.

Source: Internet
Author: User

One, you need to prepare the knowledge reserve
    1. Use node's package management tool NPM to install plug-ins, middleware basic knowledge;
      2.express framework of some basic knowledge, know how to build a small server; know how to quickly build an express framework for small applications;
      3. There is also a need for some front-end basics, html\css\js\jquery
      4. The most important thing is to know how to create a cross-domain, if you do not know how to generate cross-domain, how to know the need to crack it?
Second, the example code Analysis scene analysis, my local domain name is
<!DOCTYPE html>
App.js page code (Express generated App.js)
var createerror = require (' http-errors '); var express = require (' Express '); var path = require (' path '); var cookieparser = re Quire (' Cookie-parser '); var logger = require (' Morgan '); var indexrouter = require ('./routes/index '); var usersrouter = Require ('./routes/users '), var app = Express (), var proxymiddleware = require (' Http-proxy-middleware ');//View engine Setupapp.set (' Views ', Path.join (__dirname, ' views ')), App.set (' View engine ', ' Ejs ');//App.use (function (req, res, next ) {//Console.log (Req.query.__method, Req.method);//Req.old = req.method;//Req.method = req.query.__method;/ /next ();/}) App.use (Logger (' dev ')), App.use (Express.json ()); App.use (express.urlencoded ({extended:false})); app.u Se (Cookieparser ()); App.use (Express.static (Path.join (__dirname, ' public ')); app.middleware = [Proxymiddleware (['/ api/message/alllist/* '], {target: ' http://***.com ',//address to be proxied changeorigin:true})];app.use (App.middlewa RE);//catch 404 and forward to error Handlerapp. Use (function (req, res, next) {Next (Createerror (404));}); /Error Handlerapp.use (function (err, req, res, next) {//Set locals, only providing error in development res.locals    . message = Err.message; Res.locals.error = Req.app.get (' env ') = = = ' Development '?    ERR: {};    Render the error page Res.status (Err.status | | 500); Res.render (' Error ');}); Module.exports = app;
Catalog pictures

Own plain English explanation:

First, you install Http-proxy-middleware, and npm install http-proxy-middleware --save-dev then you reference the page.
app.middleware = [ proxyMiddleware([‘/api/message/alllist/*‘], { target: ‘http://***.com‘, changeOrigin: true }) ];Proxy the server to this address and match the route/api/message/alllist/*
Start the server, you can use node's own command node App.js or you can use the command expess in the framework of NPM Start (Package.json), this time the terminal is displayed as

Finally, enter the address in the browser: localhost:3100, note that the address in this is the address of the server you launched yourself and the port; My port number is 3100.

View terminal changes after the visit, display the request successfully

The data you requested at this point is the data on the server you are acting on, and this is my insight into cross-domain requests with node +express+http-proxy-middleware, if there is something wrong with the great God, and welcome to the same-minded comrades in the study of technology.

Use node + Express + Http-proxy-middleware to implement a detailed instance of the front end agent cross-domain.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.