美文网首页
Node接口代理方案

Node接口代理方案

作者: 明训 | 来源:发表于2021-04-21 23:21 被阅读0次

背景说明

前后端分离项目要解决的第一个问题前端本地开发如何解决ajax的跨域问题,一般情况下都是使用Nginx配置反向代理进行解决,另外还有一种方案是通过nodejs进行接口代理,现在前端的开发都是基于nodejs进行开发,保持了技术栈的统一。

官网文档

https://www.npmjs.com/package/http-proxy-middleware

https://github.com/chimurai/http-proxy-middleware

解决方案

服务源码

DeptController

@RestController
@RequestMapping("/dept")
public class DeptController {

    @GetMapping("/test")
    public String test(){
        return "/dept/test";
    }
    
}

UserController

@RestController
@RequestMapping("/user")
public class UserController {

    @GetMapping("/test")
    public String test(){
        return "/user/test";
    }

}

代理代码

创建工程svc-proxy

PS D:\study\proxy> npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.

See `npm help init` for definitive documentation on these fields
and exactly what they do.

Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.
package name: (proxy) svc-proxy
version: (1.0.0)
description: rest api proxy
entry point: (index.js)
test command:
git repository:
keywords:
author:
license: (ISC)
About to write to D:\study\proxy\package.json:

{
  "name": "svc-proxy",
  "version": "1.0.0",
  "description": "rest api proxy",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}

Is this OK? (yes)
PS D:\study\proxy>

安装模块

PS D:\study\proxy> npm i http-proxy-middleware -D
PS D:\study\proxy> npm i express -D

编写源码文件index.js

var express = require("express");
var {createProxyMiddleware}= require('http-proxy-middleware');

var app = express();

app.use(
    createProxyMiddleware('/dept',{
        "target": 'http://localhost:8080'
    })
);

app.use(
    createProxyMiddleware('/user/**',{
        
        "target": 'http://localhost:8080'
    })
);

app.listen(3000);

运行项目

PS D:\study\proxy> node index.js
[HPM] Proxy created: /dept  -> http://localhost:8080
[HPM] Proxy created: /user/**  -> http://localhost:8081

访问链接结果如下所示:

Get http://localhost:3000/dept/test
/dept/test
Get http://localhost:3000/user/test
/user/test

由上可以看出代理可以正常使用

参考文档

https://www.cnblogs.com/zhaoweikai/p/9969282.html
https://juejin.cn/post/6844903829939437581

高级方案

如果项目使用webpack作为构建工具的话,可以引入webpack-dev-server进行配置跨域方案,webpack-dev-server是一个小型的nodejs服务器,是基于express框架的,用于实时监听和打包编译静态资源,其中有一个属性是proxy,是专门用来配置代理请求接口的,webpack-dev-server的代理方案是引用了http-proxy-middleware模块进行设置的。

相关文章

  • Node接口代理方案

    背景说明 前后端分离项目要解决的第一个问题前端本地开发如何解决ajax的跨域问题,一般情况下都是使用Nginx配置...

  • 一款简约实用的“在线接口流程测试工具”

    去年为了落地“基于代理服务的接口合并方案”,实用 Node 开发了一个 freedom-api 模块,最近灵光一闪...

  • 技术方案选型

    服务端 1. 接口 Node JS(或者PHP) + MySQL方案 Node JS开发接口高效、并发性强,适合并...

  • 飞冰接口代理方案

    背景说明 使用飞冰框架进行开发过程中,发现接口请求存在跨域问题,官方提供了比较好的解决方案,这里记录如下: 解决方...

  • 搭建小型 node 服务代理接口

    通过express框架,启一个小型node服务,来模拟后端请求,并代理打包编译好的vue项目。 mock数据 数据...

  • 前端解决方案列表

    基于 Node.js 的前后端分离解决方案 基于 Webpack 的前端模块化解决方案 基于 JWT 的接口鉴权解...

  • Java中3种代理总结

    1、JDK静态代理 业务接口 接口的实现类 代理类,实现接口,并扩展实现类的功能 2、JDK动态代理 业务接口 实...

  • spring aop

    JDK动态代理和CGLib代理 JDK的代理代理类 被代理接口 被代理实现类 启动类: jdk的动态代理是针对接口...

  • Spring中默认使用jdk代理还是cglib代理?

    jdk代理(基于接口):如果目标实现了接口,那么默认使用jdk代理。cglib代理(基于类):如果目标没有实现接口...

  • 代理模式

    代理模式主要分为:静态代理,动态代理(JDK代理,接口代理)和Cglib代理(在内存中动态创建对象,不需要实现接口...

网友评论

      本文标题:Node接口代理方案

      本文链接:https://www.haomeiwen.com/subject/ikitrltx.html