惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

J
Java Code Geeks
量子位
腾讯CDC
A
About on SuperTechFans
小众软件
小众软件
Microsoft Azure Blog
Microsoft Azure Blog
T
Tailwind CSS Blog
V
V2EX
B
Blog RSS Feed
H
Hackread – Cybersecurity News, Data Breaches, AI and More
GbyAI
GbyAI
Recent Announcements
Recent Announcements
Microsoft Security Blog
Microsoft Security Blog
博客园 - 叶小钗
罗磊的独立博客
宝玉的分享
宝玉的分享
WordPress大学
WordPress大学
大猫的无限游戏
大猫的无限游戏
IT之家
IT之家
V
Visual Studio Blog
D
DataBreaches.Net
博客园 - 三生石上(FineUI控件)
月光博客
月光博客
有赞技术团队
有赞技术团队

Node.js

有深度使用 bunjs 的佬吗, rust 重写的 1.4 版本内存泄露解决了吗? - V2EX 告别 Django Admin!这个 NodeJS 全栈框架让你在 DTO 中直接配置 Table/Form 渲染 Bun 用 Rust 重写的 PR 已合并到主分支 TanStack 最新版本被投毒,病毒会盗密钥和报复性删除用户目录 Bun 从 Zig 到 Rust 的迁移已经实锤,下个版本可能成为最后一个 Zig 版本 bun 要从 zig 迁移 rust 了, 100 万刀被 claude 收购了,开始影响语言决策了 JS/Node 已经是新时代的 Java 了吧 Node.js 服务大部分时候连阿里云 Redis 是正常的,但是又时候会突然报 read ECONNRESET? 2026 年, node 写后端你用的 nestjs, fastify, honojs 还是其他? npmmirror 竟然有些包没有更新到最新版本 大家 typescript 下用的最多的是后端框架是哪个? 求助,大家 node.js 是怎么代理的 ai 时代, node.js 成为核心语言 NestJS + Swagger UI:非 200 状态码 Execute 时返回值不显示问题 Anthropic 收购 Bun 关于 Node.js 中的事件循环问题。 Hoa - 一个极简 Web 框架 有没有 v 友遇到 windows10 丢失系统环境变量的问题 Esbuild 进程占用高 有没有推荐的 Nodejs 的 sass 多租户系统 2025 年 node 项目,乱成一锅粥的 typescript ESM import 写法该怎么选? [EvanNav 6.3.1] 如何删除加载页面,让 Nav 更符合你的需求⁉️ 删除 node_modules 文件夹非常耗时 Node.JS 作者 Ryan Dahl 的故事 Vona ORM 文档终于肝完了,欢迎拍砖 Node.js 官网更新了 adonisjs 有没有现成的注册登录库? 现在流行的 Node.js 做后台比传统的 Java .Net 有哪些优势? 现在大家开发 api 都用什么 node 框架?有没有想 rails 一样,功能齐全的框架? Prisma 不能优雅的支持 DTO,可以试试 Vona ORM
深入 alova3 服务端能力:分布式 BFF 层到 API 网关的最佳实践
ScottHU · 2025-12-04 · via Node.js

可能大家对 alova 还停留在轻量化的请求策略库的层面,这当然是 alova2 的核心特点,比如以下这段

const { loading, data, error } = useRequest(() => alovaInstance.Get('/xxx'))

这是一段 alova 在客户端使用的典型代码,不过现在 alova 已经更新到 3 了,当然这些 client strategies 依然是原汁原味的,不过它不仅局限于客户端,而是在服务端也可以游刃有余了。

在 alova3 中提供了服务端请求策略( server hooks )和 redis 、file 等服务端的存储适配器,可以让我们很方便地在服务端实现全链路的请求和转发。

我们先来看一个请求的全流程:

客户端(浏览器/App )
    → Node.js BFF 层(转换数据等)
    → API 网关(鉴权、速率限制、路由分发等)
    → 后端微服务

alova 提供的 server hook 和分布式的多级缓存,可以让我们很方便地实现以上的全部层级的请求处理。

在 BFF 层转发客户端请求

在 BFF 层中经常需要转发客户端请求到后端微服务,你可以使用配合async_hooks访问每个请求的上下文,并在 alova 的beforeRequest中添加到请求中,实现用户相关数据的转发。

import { createAlova } from 'alova';
import adapterFetch from '@alova/fetch';
import express from 'express';
import { AsyncLocalStorage } from 'node:async_hooks';

// 创建异步本地存储实例
const asyncLocalStorage = new AsyncLocalStorage();

const alovaInstance = createAlova({
  requestAdapter: adapterFetch(),
  beforeRequest(method) {
    // 从异步上下文中获取请求头并传递到下游
    const context = asyncLocalStorage.getStore();
    if (context && context.headers) {
      method.config.headers = {
        ...method.config.headers,
        ...context.headers
      };
    }
  },
  responded: {
    onSuccess(response) {
      // 数据转换处理
      return {
        data: response.data,
        timestamp: Date.now(),
        transformed: true
      };
    },
    onError(error) {
      console.error('Request failed:', error);
      throw error;
    }
  }
});

const app = express();

// 中间件里设置一次,全程自动传递
app.use((req, res, next) => {
  const context = {
    userId: req.headers['x-user-id'],
    token: req.headers['authorization']
  };
  asyncLocalStorage.run(context, next);
});

// 业务代码专注业务逻辑
app.get('/api/user-profile', async (req, res) => {
  // 不用手动传递上下文了!
  const [userInfo, orders] = await Promise.all([
    alovaInstance.Get('http://gateway.com/user/profile'),
    alovaInstance.Get('http://gateway.com/order/recent')
  ]);
  
  res.json({ user: userInfo.data, orders: orders.data });
});

API 网关中的使用场景

在网关中经常需要进行鉴权、请求速率限制以及请求分发等,alova3 的 redis 存储适配器和 rateLimiter 可以很好地实现分布式的鉴权服务和请求速率限制。

鉴权可以这么搞

如果鉴权 token 有一定的过期时间,可在网关中配置 redis 存储适配器,将 token 存储在 redis 中便于重复使用,对于单机的集群服务也可以使用@alova/storage-file文件存储适配器。

import { createAlova } from 'alova';
import RedisStorageAdapter from '@alova/storage-redis';
import adapterFetch from '@alova/fetch';
import express from 'express';

const redisAdapter = new RedisStorageAdapter({
  host: 'localhost',
  port: '6379',
  username: 'default',
  password: 'my-top-secret',
  db: 0
});

const gatewayAlova = createAlova({
  requestAdapter: adapterFetch(),
  async beforeRequest(method) {
    const newToken = await authRequest(method.config.headers['Authorization'], method.config.headers['UserId'])
    method.config.headers['Authorization'] = `Bearer ${newToken}`;
  }
  // 设置 2 级存储适配器
  l2Cache: redisAdapter,
  // ...
});

const authRequest = (token, userId) => gatewayAlova.Post('http://auth.com/auth/token', null, {
  // 设置 3 个小时的缓存,将保存在 redis 中,再次以相同参数请求会命中缓存
  cacheFor: {
    mode: 'restore',
    expire: 3 * 3600 * 1000
  },
  headers: {
    'x-user-id': userId,
    'Authorization': `Bearer ${token}`
  }
});

const app = express();

// 实现 app 接收所有请求,并转发到 alova
// 注册所有 HTTP 方法的路由
const methods = ['get', 'post', 'put', 'delete', 'patch', 'options', 'head'];
methods.forEach(method => {
  app[method]('*', async (req, res) => {
    const { method, originalUrl, headers, body, query } = req;

    // 使用 alova 发送请求
    const response = await gatewayAlova.Request({
      method: method.toLowerCase(),
      url: originalUrl,
      params: query,
      data: body,
      headers
    });
    
    // 转发响应头部
    for (const [key, value] of response.headers.entries()) {
      res.setHeader(key, value);
    }
    
    // 发送响应数据
    res.status(response.status).send(await response.json());
  });
});

app.listen(3000, () => {
  console.log('Gateway server started on port 3000');
});

当然,如果需要每次请求都重新鉴权,也可以在authRequest中去掉cacheFor关闭缓存。

限流策略

alova 的 rateLimiter 可以实现分布式的限流策略,内部使用node-rate-limiter-flexible实现,我们改造一下实现。

import { createRateLimiter } from 'alova/server';

const rateLimit = createRateLimiter({
  /**
   * 点数重置的时间,单位 ms
   * @default 4000
   */
  duration: 60 * 1000,
  /**
   * duration 内可消耗的最大数量
   * @default 4
   */
  points: 4,
  /**
   * 命名空间,多个 rateLimit 使用相同存储器时可防止冲突
   */
  keyPrefix: 'user-rate-limit',
  /**
   * 锁定时长,单位 ms ,表示当到达速率限制后,将延长[blockDuration]ms ,例如 1 小时内密码错误 5 次,则锁定 24 小时,这个 24 小时就是此参数
   */
  blockDuration: 24 * 60 * 60 * 1000
});

const methods = ['get', 'post', 'put', 'delete', 'patch', 'options', 'head'];
methods.forEach(method => {
  app[method]('*', async (req, res) => {
    const { method, originalUrl, headers, body, query } = req;

    // 在此使用 rateLimit 包裹调用即可,它将默认使用 l2Cache 存储适配器作为控制参数的存储,这边的例子会用 redis 存储适配器。
    const method = gatewayAlova.Request({
      method: method.toLowerCase(),
      url: originalUrl,
      params: query,
      data: body,
      headers
    });
    const response = await rateLimit(method, {
      key: req.ip // 使用 ip 作为追踪 key ,防止同一 ip 频繁请求
    });
    
    // ...
  });
});

第三方服务集成:令牌自动维护

和外部 API 打交道需要 access_token 管理,并且很多第三方 access_token 具有调用限制,在这里我们可以使用 alova3+redis 存储适配器来实现分布式的 access_token 生命周期自动维护,其中 redis 用于 access_token 缓存,atom hook 用于分布式更新 token 的原子性操作。

import { createAlova, queryCache } from 'alova';
import RedisStorageAdapter from '@alova/storage-redis';
import adapterFetch from '@alova/fetch';
import { atomize } from 'alova/server';

const redisAdapter = new RedisStorageAdapter({
  host: 'localhost',
  port: '6379',
  username: 'default',
  password: 'my-top-secret',
  db: 0
});
const thirdPartyAlova = createAlova({
  requestAdapter: adapterFetch(),
  async beforeRequest(method) {
    // 判断是否为第三方 API ,如果是的话则获取令牌
    if (method.meta?.isThirdPartyApi) {
      // 以原子性的方式获取令牌,防止多进程同时获取 token
      const accessTokenGetMethod = getAccessToken();
      let accessToken = await queryCache(accessTokenGetMethod);
      if (!accessToken) {
        // 获取成功后将会缓存
        accessToken = await atomize(accessTokenGetMethod);
      }
      method.config.params.access_token = accessToken;
    }
  },
  l2Cache: redisAdapter,
});

const getAccessToken = () => thirdPartyAlova.Get('http://third-party.com/token', {
  params: {
    grant_type: 'client_credentials',
    client_id: process.env.THIRD_PARTY_CLIENT_ID,
    client_secret: process.env.THIRD_PARTY_CLIENT_SECRET
  },
  cacheFor: {
    mode: 'restore',
    expire: 1 * 3600 * 1000 // 两小时缓存时间
  }
});

const getThirdPartyUserInfo = userId => thirdPartyAlova.Get('http://third-party.com/user/info', {
  params: {
    userId
  },
  meta: {
    isThirdPartyApi: true
  }
});

写在最后

除此以外,alova 还提供了分布式的验证码发送和验证、请求重试等 server hooks ,想了解更多的同学可以参考服务端请求策略

如果觉得 alova 还不错,真诚希望你可以尝试体验一下,也可以给我们来一个免费的github stars

访问 alovajs 的官网查看更多详细信息:alovajs 官网

有兴趣可以加入我们的交流社区,在第一时间获取到最新进展,也能直接和开发团队交流,提出你的想法和建议。

有任何问题,你可以加入以下群聊咨询,也可以在github 仓库中发布 Discussions,如果遇到问题,也请在github 的 issues中提交,我们会在最快的时间解决。