node上的redis调用优化示例

Node.js读写数据到influxDB,目前已经有一个库node-influx, 这个库功能很是强大,可是我我的使用这个库的时候,遇到没法解决的问题。node

使用curl均可以写数据到influxDB,可是用node-influx老是报错,搞了半天也没法解决,就索性不用它了。ios

influxDB提供HTTP的API,也就是说Node.js中的axios或[request等HTTP客户端工具是能够直接和influx交互的。axios

须要注意的一点是,写到influxDB的数据格式必须是二进制流。app

1. 字符串转二进制curl

const data = Buffer.from('mymeas,mytag=1 myfield=90')

2. 设置请求Content-Type为二进制工具

'Content-Type': 'application/octet-stream'

完整代码展现:post

\`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\](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)`  
`})`//欢迎加入全栈开发交流圈一块儿学习交流:864305860  
`.\`\`catch\`\`((err) => {`//帮助突破技术瓶颈,提高思惟能力  
\`console.log(\`\`'err'``)`  
`})`

使用axios或者requst这种底层库的好处是,你用curl作的成功的任何操做,均可以转换成axios或request的请求,而不依赖与其余库。学习

相关文章
相关标签/搜索