本文主要记述了使用格物平台对接收到的数据进行转发,将数据转发到自己使用nodejs编写的服务器端。
1 创建http客户端
2 创建数据转发
创建后记得启用规则。
3 nodejs服务端
代码:
// 导入 express
const express = require('express')
// 创建 web 服务器
const app = express()
// 解析JSON格式的数据
app.use(express.json())
// 解析表单数据
app.use(express.urlencoded({ extended: false }))
app.post('/sdata', (req, res) => {
console.log(req.body) // 取所有值
console.log(req.body.propertydata) // 取数据值
console.log(req.body.propertydata[0]) // 取某个数据值
console.log(req.body.propertydata[0]['device.prop.value']) // 取某个数据的有用值
console.log('senddata_success!')
res.send('senddata_success!')
})
// 启动 web 服务器
app.listen(9999, () => {
console.log('running success')
})
4 调试
使用格物自带的调试器调试,选择在线调试:
点击发送指令后,服务端就能接收到数据了。