Connect with Node.js
Install Client Library
npm install @tdengine/websocket
Config
Run this command in your terminal to save TDengine cloud token as variables:
- Bash
- CMD
- Powershell
export TDENGINE_CLOUD_TOKEN="<token>"
export TDENGINE_CLOUD_URL="<url>"
set TDENGINE_CLOUD_TOKEN=<token>
set TDENGINE_CLOUD_URL=<url>
$env:TDENGINE_CLOUD_TOKEN='<token>'
$env:TDENGINE_CLOUD_URL='<url>'
Replace <token> and <url> with cloud token and URL. To obtain the value of cloud token and URL, please log in TDengine Cloud and click "Programming" on left menu, then select "Node.js".
Connect
const taos = require('@tdengine/websocket');
var url = process.env.TDENGINE_CLOUD_URL;
var token = process.env.TDENGINE_CLOUD_TOKEN;
async function createConnect() {
let conn = null;
try {
let conf = new taos.WSConfig(url);
conf.setToken(token);
conn = await taos.sqlConnect(conf);
} catch (err) {
throw err;
} finally {
if (conn) {
await conn.close();
}
}
}
For how to write data and query data, please refer to Data In and Tools.
For more details about how to write or query data via REST API, please check REST API.