Methods and steps to use http-proxy-middleware to implement proxy cross-domain in Node

Methods and steps to use http-proxy-middleware to implement proxy cross-domain in Node

1. Install the proxy module

cnpm i http-proxy-middleware -S

2. Configure the proxy

const express = require('express');
const app = express();

/* Proxy configuration start */
const proxy = require('http-proxy-middleware'); //Introduce proxy module const proxyOptions = {
    target: 'http://127.0.0.1:9999', //backend server address changeOrigin: true //process cross-domain};
const exampleProxy = proxy('/api/*', proxyOptions); //Requests with the api prefix all go through the proxy app.use(exampleProxy);
/* Proxy configuration end */

const hostName = '127.0.0.1';
const port = 8080;

app.get('/', function(req, res) {

    
    const html =
    `<!DOCTYPE html>
 <html lang="en">
     <head>
         <meta charset="UTF-8" />
         <meta name="viewport" content="width=device-width, initial-scale=1.0" />
         <meta http-equiv="X-UA-Compatible" content="ie=edge" />
         <title>Document</title>
     </head>
     <body>
         <button id="btn1">Request server interface 1</button>
         <button id="btn2">Request server interface 2</button>
         <script src="https://cdn.bootcss.com/axios/0.19.0/axios.min.js"></script>
         <script>
             document.getElementById('btn1').addEventListener(
                 'click',
                 () => {
                     axios.get('/api/hello', {
                         params: {
                             key: 'hello'
                         }
                     });
                 },
                 false
             );
 
             document.getElementById('btn2').addEventListener(
                 'click',
                 () => {
                     axios.get('/api/word', {
                         params: {
                             key: 'word'
                         }
                     });
                 },
                 false
             );
         </script>
     </body>
 </html>`;

    res.setHeader('Content-Type', 'text/html');
    res.send(html);
});


app.listen(port, hostName, function() {

    console.log(`The server is running at http://${hostName}:${port}`);

});

This is the end of this article about the steps to use http-proxy-middleware in Node to implement proxy cross-domain. For more relevant Node http-proxy-middleware proxy cross-domain content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Use of node cross-domain forwarding express+http-proxy-middleware

<<:  Overview and differences between html inline elements and html block-level elements

>>:  Docker builds jenkins+maven code building and deployment platform

Recommend

Analysis of the reasons why MySQL's index system uses B+ tree

Table of contents 1. What is an index? 2. Why do ...

Mysql uses stored procedures to quickly add millions of data sample code

Preface In order to reflect the difference betwee...

Three.js realizes Facebook Metaverse 3D dynamic logo effect

Table of contents background What is the Metavers...

Detailed explanation of Cgroup, the core principle of Docker

The powerful tool cgroup in the kernel can not on...

How to understand the difference between ref toRef and toRefs in Vue3

Table of contents 1. Basics 1.ref 2. toRef 3. toR...

Solution to the problem that Centos8 cannot install docker

Problem [root@zh ~]# [root@zh ~]# [root@zh ~]# yu...

MySQL index principle and query optimization detailed explanation

Table of contents 1. Introduction 1. What is an i...

Simple understanding and examples of MySQL index pushdown (ICP)

Preface Index Condition Pushdown (ICP) is a new f...

Detailed explanation of the process of docker packaging Python environment

The steps of docker packaging Python environment ...

A brief discussion on the three major issues of JS: asynchrony and single thread

Table of contents Single thread asynchronous Sing...

How to view and set the mysql time zone

1. Check the database time zone show variables li...

Solve the problem of Mac Docker x509 certificate

question Recently I needed to log in to a private...

Detailed explanation of the role of brackets in AngularJS

1. The role of brackets 1.1 Square brackets [ ] W...

Basic usage tutorial of IPTABLES firewall in LINUX

Preface For production VPS with public IP, only t...