简单mock server模拟用户请求给小程序提供数据

news/2024/7/20 4:20:38 标签: 小程序, apache, 前端, 洗衣小程序

整理小程序代码时发现一此小程序离开了mock-server基本上没有办法显示了,因此用node,express来满足给小程序提供演示数据的功能 

const express = require('express');  
const { createCanvas, Image } = require('canvas');  
const fs = require('fs');  
const path = require('path');  
const app = express();  
const port = 3000;  
const querystring = require('querystring');  


function createImageFromPath(req, res) {

  const imagePath = req.path;  
  const match = imagePath.match(/\/(\d+)x(\d+)\.jpg/);  
  if (match) {  
    const [, widthStr, heightStr] = match;  
    const width = parseInt(widthStr, 10);  
    const height = parseInt(heightStr, 10);  
  
    if (isNaN(width) || isNaN(height)) {  
      return res.status(400).send('Invalid image dimensions');  
    }  

   
  
    const canvas = createCanvas(width, height);  
    const ctx = canvas.getContext('2d');  
  
    // 生成随机背景颜色  
    const randomColor = '#' + Math.floor(Math.random() * 16777215).toString(16);  
    ctx.fillStyle = randomColor;  
    ctx.fillRect(0, 0, width, height);  


    
  
    // 获取对比色  
    const contrastColor = getContrastColor(randomColor);  

      const queryParams = req.query.q;  
      console.log(queryParams);
        // 在图像上显示尺寸字样  
        const fontSize = 30; // 字体大小  
        let text = `${width}x${height}`; // 尺寸字样            
        if (queryParams) {  
          text = queryParams; // 如果查询参数不为空,使用查询参数的值  
        }  
        ctx.font = `${fontSize}px simsun`;  
        const metrics = ctx.measureText(text); // 测量文本尺寸  
        const x = (width - metrics.width) / 2; // 计算文本水平居中位置  
        const y = height / 2 + fontSize / 2; // 计算文本垂直居中位置  
    // 画描边  
   // ctx.strokeStyle = contrastColor;  
   // ctx.lineWidth = 10;  
   // ctx.strokeText(text, x, y);  
  
    // 画填充文本  
    ctx.fillStyle = contrastColor;  
    ctx.fillText(text, x, y);  
  
    // 画图像边框  
    ctx.strokeStyle = contrastColor;  
    ctx.lineWidth = 10; // 边框宽度  
    ctx.strokeRect(0, 0, width, height); // 画矩形边框  
  
    // 将canvas转换为Buffer对象  
    const buffer = canvas.toBuffer('image/jpeg');  
  
    // 设置响应头信息  
    res.setHeader('Content-Type', 'image/jpeg');  
    res.setHeader('Content-Disposition', 'inline; filename=generated.jpg');  
    res.setHeader('Cache-Control', 'no-store, no-cache, must-revalidate, proxy-revalidate');  
    res.setHeader('Pragma', 'no-cache');  
    res.setHeader('Expires', '0');  
  
    // 发送图片Buffer到响应中  
    res.send(buffer);  
  } else {  
    // 如果没有匹配到尺寸,则发送404错误  
    res.status(404).send('Not Found');  
  }  
}  
  
// 辅助函数:获取对比色  
function getContrastColor(color) {  
  // 将颜色字符串转换为RGB数组  
  const rgb = color.slice(1).match(/.{2}/g).map(byte => parseInt(byte, 16));  
  
  // 计算亮度  
  const luminance = (0.299 * rgb[0] + 0.587 * rgb[1] + 0.114 * rgb[2]) / 255;  
  
  // 根据亮度返回对比色  
  return luminance > 0.5 ? '#000000' : '#FFFFFF';  
}

  
  // 使用中间件处理所有以.jpg结尾的请求  
app.use(express.static(path.join(__dirname, 'public'))); // 假设你的静态文件在public文件夹中  
app.get(/\/(\d+)x(\d+)\.jpg/, createImageFromPath);  
app.get(/\*.jpg$/, (req, res) => {  
  res.status(404).send('Not Found');  
});  
  
  
app.get('/:filename', (req, res) => {  
  const fileName = req.params.filename;  
  const filePath = path.join(__dirname, `${fileName}.json`);  
  
  fs.readFile(filePath, 'utf8', (err, data) => {  
    if (err) {  
      if (err.code === 'ENOENT') {  
        // 文件不存在的错误  
        res.status(404).send('File not found');  
      } else {  
        // 其他类型的错误  
        res.status(500).send('Internal Server Error');  
      }  
      return;  
    }  
  
    res.setHeader('Content-Type', 'application/json');  
    res.send(data);  
  });  
});  
app.listen(port, () => {  
  console.log(`Server is running on port ${port}`);  
});

整理一个o2o行业  洗衣小程序时添加了一些演示数据,这个洗衣程序, 有充值页面, 在地图搜索洗衣机, 拖动地图时, 可以实时加载洗衣机, 可以绑定洗衣, 没有后台, 只有简单的mock server, 看了一下这是一个未完成的项目, 感兴趣的话, 可以动手完善一下, 

下面是一些程序的截图


http://www.niftyadmin.cn/n/5391755.html

相关文章

编译GCC native编译器的几点启示

启示 编译 GCC native compiler按照官方介绍并不难 步骤见后面实践脚本,以及官方编译指南链接 GCC编译器编译其它程序组件时,会优先使用自身携带的库,例如,常用的自带库,libgcc_s.so、libstdc 如果部署环境与编译要求…

【漏洞复现】大华智慧园区综合管理平台信息泄露漏洞

Nx01 产品简介 大华智慧园区综合管理平台是一款综合管理平台,具备园区运营、资源调配和智能服务等功能。该平台旨在协助优化园区资源分配,满足多元化的管理需求,同时通过提供智能服务,增强使用体验。 Nx02 漏洞描述 大华智慧园区…

C++入门学习(三十四)值传递,实参-形参

什么是值传递? 值传递(Pass by Value)是一种参数传递方式,当函数或方法被调用时,将实际参数的值复制一份传递给函数或方法中的形式参数。这意味着在函数或方法内部对形式参数的修改不会影响到实际参数的值。因为形式参…

鸿蒙LiteOS-M 内核初始化

目录 一、LiteOS-M 初始化内核二、LOS_KernelInit代码分析三、LOS_Start代码解析坚持就有收获 一、LiteOS-M 初始化内核 在LiteOS-M应用程序中,系统初始化如下: /*** brief This is the ohos entry, and you could call this in your main funciton af…

Springboot+vue的社区医疗综合服务平台(有报告)。Javaee项目,springboot vue前后端分离项目

演示视频: Springbootvue的社区医疗综合服务平台(有报告)。Javaee项目,springboot vue前后端分离项目 项目介绍: 本文设计了一个基于Springbootvue的前后端分离的社区医疗综合服务平台,采用M(m…

在Ubuntu系统中,特定版本的pip来为特定版本的Python安装依赖包

1.使用python3.x版本的pip: 如果你系统上有多个Python版本,每个版本都有对应的pip,你可以使用具体版本的pip来安装依赖包。例如,如果你想为Python 3.8安装依赖包,可以使用以下命令: python3.8 -m pip ins…

[概念区分] 正则表达式与正则化

正则表达式与正则化 机器学习在计算机科学和数据处理领域,关于“正则”的两个术语:正则表达式和正则化,虽然它们在名称上非常相似,但实际上它们是完全不同的概念。 正则表达式 也被称为 regex,是一种强大的工具&…

一周学会Django5 Python Web开发-Http请求HttpRequest请求类

锋哥原创的Python Web开发 Django5视频教程: 2024版 Django5 Python web开发 视频教程(无废话版) 玩命更新中~_哔哩哔哩_bilibili2024版 Django5 Python web开发 视频教程(无废话版) 玩命更新中~共计25条视频,包括:2024版 Django5 Python we…