0.6.10 • Published 4 months ago

http2mqtt v0.6.10

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
4 months ago

http2mqtt

简介

使用http请求同步发送mqtt消息

http2mqtt

接口文档

在线接口文档参考

快速开始

/* eslint-disable no-console */
import Koa from 'koa'
import Router from 'koa-router'
import bodyParser from 'koa-bodyparser'

import { Headers, Body, Http2Mqtt, ResponsePayload, Query } from '../src/http-to-mqtt.js'

// 运行npm i http2mqtt安装依赖
// import { Headers, Body, Http2Mqtt, ResponsePayload, Query } from 'http2mqtt'

const app = new Koa()
const router = new Router()

// 使用bodyParser中间件处理请求体
app.use(bodyParser())

router.post('/mqtt', async (ctx) => {
  // 将请求的上下文打印到控制台,方便调试
  console.debug(JSON.stringify(ctx))

  // 将请求头部的键转换为小写,以确保统一性
  const headers: Headers = Object.fromEntries(
    Object.entries(ctx.request.headers).map(([ key, value ]) => [ key.toLowerCase(), value ]),
  )  as unknown as Headers

  const body: Body = ctx.request.body as Body
  const query: Query = ctx.request.query as unknown as Query

  const ops = { body, headers, query }
  console.debug(JSON.stringify(ops))

  // 使用Http2Mqtt类处理请求
  const http2mqtt = new Http2Mqtt(ops)
  const res: ResponsePayload = await http2mqtt.pubMessage()

  // 设置响应的状态码和主体
  ctx.status = res.status
  ctx.body = res.body
})

// 使用路由中间件
app.use(router.routes()).use(router.allowedMethods())

// 从环境变量中获取端口号,如果没有定义,则默认为3000
const PORT = process.env['PORT'] || 3000

// 启动Koa服务器
app.listen(PORT, () => {
  console.log(`Server running on port ${PORT}`)
})

启动服务

npm run start:koa

请求示例

  • 请求
POST /mqtt?RequestTopic=my/topic&Convert={"transformed":$.original}
Headers:
  endpoint: mqtt.example.com
  username: myuser
  password: mypassword

Body:
{
  "original": "data"
}
  • 响应
{
  "error": "ok",
  "message": {
    "transformed": "data"
  }
}

百度云部署

cd examples/baidu-cfc
npm i

将index.js、package.json以及node_modules文件夹整理打包上传到百度云函数计算,设置http触发器

历史版本

main v0.3.0

  1. 固化参数

  2. 百度云示例代码、koa示例代码等

  3. 增加api接口说明

v0.2.0

  1. 构建流水线自动发包

v0.0.1 (2023-8-31)

  1. 初始化创建代码库及npm包

Copyright & License

  • Code released under the Apache-2.0 License
  • Docs released under Creative Commons
0.6.7

4 months ago

0.6.6

4 months ago

0.6.10

4 months ago

0.6.11

4 months ago

0.6.3

4 months ago

0.6.4

4 months ago

0.5.10

4 months ago

0.5.11

4 months ago

0.5.8

4 months ago

0.5.7

4 months ago

0.5.9

4 months ago

0.5.14

4 months ago

0.5.12

4 months ago

0.5.13

4 months ago

0.5.4

4 months ago

0.6.2

4 months ago

0.5.3

4 months ago

0.5.6

4 months ago

0.5.5

4 months ago

0.6.1

4 months ago

0.5.1

4 months ago

0.4.1

7 months ago

0.4.2

7 months ago

0.3.0

8 months ago

0.3.6

8 months ago

0.3.8

8 months ago

0.3.7

8 months ago

0.4.0

8 months ago

0.3.1

8 months ago

0.3.4

8 months ago

0.3.3

8 months ago

0.2.2

9 months ago

0.2.1

9 months ago

0.2.0

9 months ago

0.1.4

9 months ago

0.1.3

9 months ago

0.1.2

9 months ago