以下是一个简单的可以被调用的图片接口的示例:

// 定义一个函数,生成图片地址
function generateImageUrl(width, height) {
  return `https://picsum.photos/${width}/${height}`;
}

// 定义一个路由,处理图片请求
app.get('/api/image', (req, res) => {
  // 从请求参数中获取图片宽度和高度
  const width = req.query.width || 200;
  const height = req.query.height || 200;

  // 生成图片地址
  const imageUrl = generateImageUrl(width, height);

  // 发送图片
  res.sendFile(imageUrl);
});

这个接口允许客户端通过请求 /api/image?width=300&height=400 来获取一张宽度为 300,高度为 400 的随机图片。如果没有指定宽度和高度,则默认为 200

用js写一个可以被调用的图片接口

原文地址: https://www.cveoy.top/t/topic/dwJ1 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录