Node.js讀寫數據到influxDB,目前已經有一個庫node-influx, 這個庫功能很是強大,可是我我的使用這個庫的時候,遇到沒法解決的問題。node
使用curl均可以寫數據到influxDB,可是用node-influx老是報錯,搞了半天也沒法解決,就索性不用它了。ios
influxDB提供HTTP的API,也就是說Node.js中的axios或者request等HTTP客戶端工具是能夠直接和influx交互的。git
須要注意的一點是,寫到influxDB的數據格式必須是二進制流。github
爲此,要作兩件事情:axios
1. 字符串轉二進制
app
const data = Buffer.from('mymeas,mytag=1 myfield=90')
2. 設置請求Content-Type爲二進制
curl
'Content-Type': 'application/octet-stream'
完整代碼
工具
const axios = require('axios') const data = Buffer.from('mylog,name=wdd error_count=2003,no_send=0') axios({ url: 'http://localhost:8923/write?db=poc&rp=poc', method: 'post', headers: { 'Content-Type': 'application/octet-stream' }, data: data }) .then((res) => { console.log('ok') // console.log(res) }) .catch((err) => { console.log('err') })
使用axios或者requst這種底層庫的好處是,你用curl作的成功的任何操做,均可以轉換成axios或request的請求,而不依賴與其餘庫。
post